46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
import pandas as pd
|
|
import requests
|
|
import json
|
|
from datetime import datetime
|
|
|
|
# Excel 文件路径
|
|
EXCEL_FILE = "/home/xsl/code/xsl_node/docs/example.xls"
|
|
|
|
# API 端点
|
|
API_URL = "http://localhost:8000/api/projects"
|
|
|
|
# xsl 用户的 token
|
|
TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ4c2wiLCJleHAiOjE3Njk0MjIxOTh9.5f_hiybhIRl-biiLETczZwLr9E-LcAxhi57Cc7ikEyI"
|
|
|
|
# 读取 Excel 文件,跳过前 4 行表头
|
|
df = pd.read_excel(EXCEL_FILE, skiprows=4)
|
|
|
|
# 处理数据并导入项目
|
|
for index, row in df.iterrows():
|
|
# 跳过空行
|
|
if pd.isna(row.iloc[0]):
|
|
continue
|
|
|
|
# 构建项目数据
|
|
project_data = {
|
|
"name": str(row.iloc[2]) if pd.notna(row.iloc[2]) else f"Project {index+1}",
|
|
"description": str(row.iloc[3]) if pd.notna(row.iloc[3]) else "",
|
|
"department": "Marketing",
|
|
"budget": int(row.iloc[4]) if pd.notna(row.iloc[4]) else None,
|
|
"status": "pending"
|
|
}
|
|
|
|
# 发送 POST 请求创建项目
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"Authorization": f"Bearer {TOKEN}"
|
|
}
|
|
|
|
response = requests.post(API_URL, headers=headers, data=json.dumps(project_data))
|
|
|
|
if response.status_code == 200:
|
|
print(f"Project '{project_data['name']}' created successfully!")
|
|
else:
|
|
print(f"Failed to create project '{project_data['name']}': {response.status_code} - {response.text}")
|
|
|
|
print("\nProject import completed!") |