{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import json\n", "import xlrd\n", "import xlwt\n", "from datetime import datetime, timedelta \n", "import time\n", "import pandas as pd\n", "import numpy as np\n", "# 变量定义\n", "login_url = \"http://10.200.32.39/jingbo-api/api/server/login\"\n", "search_url = \"http://10.200.32.39/jingbo-api/api/warehouse/dwDataItem/queryByItemNos\"\n", "\n", "login_push_url = \"http://10.200.32.39/jingbo-api/api/server/login\"\n", "upload_url = \"http://10.200.32.39/jingbo-api/api/dw/dataValue/pushDataValueList\"\n", "\n", "login_data = {\n", " \"data\": {\n", " \"account\": \"api_dev\",\n", " \"password\": \"ZTEwYWRjMzk0OWJhNTlhYmJlNTZlMDU3ZjIwZjg4M2U=\",\n", " \"tenantHashCode\": \"8a4577dbd919675758d57999a1e891fe\",\n", " \"terminal\": \"API\"\n", " },\n", " \"funcModule\": \"API\",\n", " \"funcOperation\": \"获取token\"\n", "}\n", "\n", "login_push_data = {\n", " \"data\": {\n", " \"account\": \"api_dev\",\n", " \"password\": \"ZTEwYWRjMzk0OWJhNTlhYmJlNTZlMDU3ZjIwZjg4M2U=\",\n", " \"tenantHashCode\": \"8a4577dbd919675758d57999a1e891fe\",\n", " \"terminal\": \"API\"\n", " },\n", " \"funcModule\": \"API\",\n", " \"funcOperation\": \"获取token\"\n", "}\n", "\n", "read_file_path_name = \"定性模型数据项12-11.xls\"\n", "one_cols = []\n", "two_cols = []\n", "\n", "\n", "\n", "\n", "def start(date=''):\n", " workbook = xlrd.open_workbook(read_file_path_name)\n", "\n", "\n", "\n", " # 选择第一个表格\n", " sheet = workbook.sheet_by_index(0)\n", "\n", " # 获取行数和列数\n", " num_rows = sheet.nrows\n", "\n", "\n", "\n", " row_data = sheet.row_values(1)\n", " one_cols = row_data\n", "\n", "\n", " login_res = requests.post(url=login_url, json=login_data, timeout=(3, 5))\n", " text = json.loads(login_res.text)\n", " if text[\"status\"]:\n", " token = text[\"data\"][\"accessToken\"]\n", " else:\n", " print(\"获取认证失败\")\n", " token = None\n", "\n", " if date == '':\n", " now = datetime.now()\n", " else:\n", " now = date\n", " year = now.year\n", " month = now.month\n", " day = now.day\n", "\n", " if month < 10:\n", " month = \"0\" + str(month)\n", " if day < 10:\n", " day = \"0\" + str(day)\n", " cur_time = str(year) + str(month) + str(day)\n", " cur_time2 = str(year) + \"-\" + str(month) + \"-\" + str(day)\n", " search_data = {\n", " \"data\": {\n", " \"date\": cur_time,\n", " \"dataItemNoList\": one_cols[1:]\n", " },\n", " \"funcModule\": \"数据项\",\n", " \"funcOperation\": \"查询\"\n", " }\n", " headers = {\"Authorization\": token}\n", " search_res = requests.post(url=search_url, headers=headers, json=search_data, timeout=(3, 5))\n", " search_value = json.loads(search_res.text)[\"data\"]\n", "# datas = search_value\n", " if search_value:\n", " datas = search_value\n", " else :\n", " datas = None\n", " \n", "\n", " append_rows = [cur_time2]\n", " dataItemNo_dataValue = {}\n", "# for data_value in datas:\n", "# dataItemNo_dataValue[data_value[\"dataItemNo\"]] = data_value[\"dataValue\"]\n", " for data_value in datas:\n", " if \"dataValue\" not in data_value:\n", " print(data_value)\n", " dataItemNo_dataValue[data_value[\"dataItemNo\"]] = \"\"\n", " else:\n", " dataItemNo_dataValue[data_value[\"dataItemNo\"]] = data_value[\"dataValue\"]\n", " for value in one_cols[1:]:\n", " if value in dataItemNo_dataValue:\n", " append_rows.append(dataItemNo_dataValue[value])\n", " else:\n", " append_rows.append(\"\")\n", "\n", " workbook = xlrd.open_workbook('定性模型数据项12-11.xls')\n", "\n", " # 获取所有sheet的个数\n", " sheet_count = len(workbook.sheet_names())\n", "\n", " # 获取所有sheet的名称\n", " sheet_names = workbook.sheet_names()\n", "\n", " new_workbook = xlwt.Workbook()\n", " for i in range(sheet_count):\n", " # 获取当前sheet\n", " sheet = workbook.sheet_by_index(i)\n", "\n", " # 获取sheet的行数和列数\n", " row_count = sheet.nrows\n", " col_count = sheet.ncols\n", " # 获取原有数据\n", " data = []\n", " for row in range(row_count):\n", " row_data = []\n", " for col in range(col_count):\n", " row_data.append(sheet.cell_value(row, col))\n", " data.append(row_data)\n", " # 创建xlwt的Workbook对象\n", " # 创建sheet\n", " new_sheet = new_workbook.add_sheet(sheet_names[i])\n", "\n", " # 将原有的数据写入新的sheet\n", " for row in range(row_count):\n", " for col in range(col_count):\n", " new_sheet.write(row, col, data[row][col])\n", "\n", " if i == 0:\n", " # 在新的sheet中添加数据\n", " for col in range(col_count):\n", " new_sheet.write(row_count, col, append_rows[col])\n", "\n", " # 保存新的xls文件\n", " new_workbook.save(\"定性模型数据项12-11.xls\")\n", "\n", " df = pd.read_excel('定性模型数据项12-11.xls')\n", " df=df.fillna(df.ffill())\n", " df1 = df[-2:].reset_index()\n", " if df1.loc[1,'70号沥青开工率'] > 0.3:\n", " a = (df1.loc[1,'70号沥青开工率']-0.2)*5/0.1\n", " else :\n", " a = 0\n", " b = df1.loc[1,'资金因素']\n", " if df1.loc[1,'昨日计划提货偏差']>0:\n", " c = df1.loc[1,'昨日计划提货偏差']*10/2000\n", " else :\n", " c = df1.loc[1,'昨日计划提货偏差']*10/3000\n", " d = df1.loc[1,'生产情况']\n", " if df1.loc[1,'基质沥青库存']/265007 >0.8:\n", " e = (df1.loc[1,'基质沥青库存'] - df1.loc[0,'基质沥青库存'])*10/-5000\n", " else : \n", " e = 0\n", " f = df1.loc[1,'下游客户价格预期']\n", " if abs(df1.loc[1,'即期成本'] - df1.loc[0,'即期成本'])>=100:\n", " g = (df1.loc[1,'即期成本'] - df1.loc[0,'即期成本'])*50/100\n", " else :\n", " g = 0\n", " h = df1.loc[1,'订单结构']\n", " x = round(0.08*a+0*b+0.15*c+0.08*d +0.03*e +0.08*f +0.4*g+0.18*h+df1.loc[0,'京博指导价'],2)\n", "\n", "\n", " login_res1 = requests.post(url=login_push_url, json=login_push_data, timeout=(3, 5))\n", " text1 = json.loads(login_res1.text)\n", " token_push = text1[\"data\"][\"accessToken\"]\n", "\n", "\n", " data1 = {\n", " \"funcModule\": \"数据表信息列表\",\n", " \"funcOperation\": \"新增\",\n", " \"data\": [\n", " {\"dataItemNo\": \"C01100036|Forecast_Price|DX|ACN\",\n", " \"dataDate\": cur_time,\n", " \"dataStatus\": \"add\",\n", " \"dataValue\": x\n", " }\n", "\n", " ]\n", " }\n", " headers1 = {\"Authorization\": token_push}\n", " res = requests.post(url=upload_url, headers=headers1, json=data1, timeout=(3, 5))\n", " \n", " \n", " \n", " \n", "def start_1():\n", " workbook = xlrd.open_workbook(read_file_path_name)\n", "\n", "\n", "\n", " # 选择第一个表格\n", " sheet = workbook.sheet_by_index(0)\n", "\n", " # 获取行数和列数\n", " num_rows = sheet.nrows\n", "\n", "\n", "\n", " row_data = sheet.row_values(1)\n", " one_cols = row_data\n", "\n", "\n", " login_res = requests.post(url=login_url, json=login_data, timeout=(3, 5))\n", " text = json.loads(login_res.text)\n", " if text[\"status\"]:\n", " token = text[\"data\"][\"accessToken\"]\n", " else:\n", " print(\"获取认证失败\")\n", " token = None\n", "\n", "\n", " now = datetime.now() - timedelta(days=1) \n", " year = now.year\n", " month = now.month\n", " day = now.day\n", "\n", " if month < 10:\n", " month = \"0\" + str(month)\n", " if day < 10:\n", " day = \"0\" + str(day)\n", " cur_time = str(year) + str(month) + str(day)\n", " cur_time2 = str(year) + \"-\" + str(month) + \"-\" + str(day)\n", " search_data = {\n", " \"data\": {\n", " \"date\": cur_time,\n", " \"dataItemNoList\": one_cols[1:]\n", " },\n", " \"funcModule\": \"数据项\",\n", " \"funcOperation\": \"查询\"\n", " }\n", " headers = {\"Authorization\": token}\n", " search_res = requests.post(url=search_url, headers=headers, json=search_data, timeout=(3, 5))\n", " search_value = json.loads(search_res.text)[\"data\"]\n", "# datas = search_value\n", " if search_value:\n", " datas = search_value\n", " else :\n", " datas = None\n", " \n", " \n", "\n", " append_rows = [cur_time2]\n", " dataItemNo_dataValue = {}\n", "# for data_value in datas:\n", "# dataItemNo_dataValue[data_value[\"dataItemNo\"]] = data_value[\"dataValue\"]\n", " for data_value in datas:\n", " if \"dataValue\" not in data_value:\n", " print(data_value)\n", " dataItemNo_dataValue[data_value[\"dataItemNo\"]] = \"\"\n", " else:\n", " dataItemNo_dataValue[data_value[\"dataItemNo\"]] = data_value[\"dataValue\"]\n", " for value in one_cols[1:]:\n", " if value in dataItemNo_dataValue:\n", " append_rows.append(dataItemNo_dataValue[value])\n", " else:\n", " append_rows.append(\"\")\n", "\n", " workbook = xlrd.open_workbook('定性模型数据项12-11.xls')\n", "\n", " # 获取所有sheet的个数\n", " sheet_count = len(workbook.sheet_names())\n", "\n", " # 获取所有sheet的名称\n", " sheet_names = workbook.sheet_names()\n", "\n", " new_workbook = xlwt.Workbook()\n", " for i in range(sheet_count):\n", " # 获取当前sheet\n", " sheet = workbook.sheet_by_index(i)\n", "\n", " # 获取sheet的行数和列数\n", " row_count = sheet.nrows - 1\n", " col_count = sheet.ncols\n", " # 获取原有数据\n", " data = []\n", " for row in range(row_count):\n", " row_data = []\n", " for col in range(col_count):\n", " row_data.append(sheet.cell_value(row, col))\n", " data.append(row_data)\n", " # 创建xlwt的Workbook对象\n", " # 创建sheet\n", " new_sheet = new_workbook.add_sheet(sheet_names[i])\n", "\n", " # 将原有的数据写入新的sheet\n", " for row in range(row_count):\n", " for col in range(col_count):\n", " new_sheet.write(row, col, data[row][col])\n", "\n", " if i == 0:\n", " # 在新的sheet中添加数据\n", " for col in range(col_count):\n", " new_sheet.write(row_count, col, append_rows[col])\n", "\n", " # 保存新的xls文件\n", " new_workbook.save(\"定性模型数据项12-11.xls\")\n", "\n", "\n", "def start_2(date):\n", " workbook = xlrd.open_workbook(read_file_path_name)\n", "\n", "\n", "\n", " # 选择第一个表格\n", " sheet = workbook.sheet_by_index(0)\n", "\n", " # 获取行数和列数\n", " num_rows = sheet.nrows\n", "\n", "\n", "\n", " row_data = sheet.row_values(1)\n", " one_cols = row_data\n", "\n", "\n", " login_res = requests.post(url=login_url, json=login_data, timeout=(3, 5))\n", " text = json.loads(login_res.text)\n", " if text[\"status\"]:\n", " token = text[\"data\"][\"accessToken\"]\n", " else:\n", " print(\"获取认证失败\")\n", " token = None\n", "\n", "\n", " now = date\n", " year = now.year\n", " month = now.month\n", " day = now.day\n", "\n", " if month < 10:\n", " month = \"0\" + str(month)\n", " if day < 10:\n", " day = \"0\" + str(day)\n", " cur_time = str(year) + str(month) + str(day)\n", " cur_time2 = str(year) + \"-\" + str(month) + \"-\" + str(day)\n", " search_data = {\n", " \"data\": {\n", " \"date\": cur_time,\n", " \"dataItemNoList\": one_cols[1:]\n", " },\n", " \"funcModule\": \"数据项\",\n", " \"funcOperation\": \"查询\"\n", " }\n", " headers = {\"Authorization\": token}\n", " search_res = requests.post(url=search_url, headers=headers, json=search_data, timeout=(3, 5))\n", " search_value = json.loads(search_res.text)[\"data\"]\n", "# datas = search_value\n", " if search_value:\n", " datas = search_value\n", " else :\n", " datas = None\n", " \n", "\n", " append_rows = [cur_time2]\n", " dataItemNo_dataValue = {}\n", "# for data_value in datas:\n", "# dataItemNo_dataValue[data_value[\"dataItemNo\"]] = data_value[\"dataValue\"]\n", " for data_value in datas:\n", " if \"dataValue\" not in data_value:\n", " print(data_value)\n", " dataItemNo_dataValue[data_value[\"dataItemNo\"]] = \"\"\n", " else:\n", " dataItemNo_dataValue[data_value[\"dataItemNo\"]] = data_value[\"dataValue\"]\n", " for value in one_cols[1:]:\n", " if value in dataItemNo_dataValue:\n", " append_rows.append(dataItemNo_dataValue[value])\n", " else:\n", " append_rows.append(\"\")\n", "\n", " workbook = xlrd.open_workbook('定性模型数据项12-11.xls')\n", "\n", " # 获取所有sheet的个数\n", " sheet_count = len(workbook.sheet_names())\n", "\n", " # 获取所有sheet的名称\n", " sheet_names = workbook.sheet_names()\n", "\n", " new_workbook = xlwt.Workbook()\n", " for i in range(sheet_count):\n", " # 获取当前sheet\n", " sheet = workbook.sheet_by_index(i)\n", "\n", " # 获取sheet的行数和列数\n", " row_count = sheet.nrows\n", " col_count = sheet.ncols\n", " # 获取原有数据\n", " data = []\n", " for row in range(row_count):\n", " row_data = []\n", " for col in range(col_count):\n", " row_data.append(sheet.cell_value(row, col))\n", " data.append(row_data)\n", " # 创建xlwt的Workbook对象\n", " # 创建sheet\n", " new_sheet = new_workbook.add_sheet(sheet_names[i])\n", "\n", " # 将原有的数据写入新的sheet\n", " for row in range(row_count):\n", " for col in range(col_count):\n", " new_sheet.write(row, col, data[row][col])\n", "\n", " if i == 0:\n", " # 在新的sheet中添加数据\n", " for col in range(col_count):\n", " new_sheet.write(row_count, col, append_rows[col])\n", "\n", " # 保存新的xls文件\n", " new_workbook.save(\"定性模型数据项12-11.xls\")\n", " print('关闭文件')\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "if __name__ == \"__main__\":\n", " pass\n", " # 需要单独运行放开\n", " \n", " # start_1()\n", "\n", " # 每天定时12点运行\n", " # while True:\n", " # # 获取当前时间\n", " # current_time = time.strftime(\"%H:%M:%S\", time.localtime())\n", " # current_time_1 = time.strftime(\"%H:%M:%S\", time.localtime())\n", "\n", " # # 判断当前时间是否为执行任务的时间点\n", " # if current_time == \"12:00:00\":\n", " # print(\"执行定时任务\")\n", " # start()\n", "\n", " # # 休眠1秒钟,避免过多占用CPU资源\n", " # time.sleep(1)\n", " \n", " # elif current_time_1 == \"20:00:00\":\n", " # print(\"更新数据\")\n", " # start_1()\n", " # time.sleep(1)\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20241031\n", "20241101\n" ] } ], "source": [ "from datetime import datetime, timedelta\n", "\n", "start_date = datetime(2024, 10, 31)\n", "end_date = datetime(2024, 11, 2)\n", "\n", "while start_date < end_date:\n", " print(start_date.strftime('%Y%m%d'))\n", " start(start_date)\n", " start_date += timedelta(days=1)\n", " \n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 4 }