[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
import os
import requests
import os.path as op
from flask import Flask, request,send_from_directory
import pandas as pd
import time
app = Flask(__name__)
@app.route('/api/send')
def uploaded_file():
print(len(request.args))
name = request.args.get('name')
pt = request.args.get('pt')
p = os.getcwd() + "/" + pt
print(p)
return send_from_directory(p,name)
@app.route('/api/get')
def testGet():
name = request.args.get('name')
print(name)
a = downloadfile(name)
return a
def downloadfile(url):
houzhui = op.splitext(url)[-1]
fname =op.splitext(op.split(url)[1])[0]
lujing = op.join(os.getcwd(), fname)
print(lujing)
if not(op.exists(lujing)):
os.mkdir(lujing, 777 );
file_to_save = op.join(lujing, str(int(round(time.time() * 1000))) + houzhui)
print(file_to_save)
response = requests.get(url, stream=True)
print(response.headers["Content-Length"])
with open(file_to_save, "wb") as f:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
print("\n结束下载")
fsize = op.getsize(file_to_save)
print(fsize)
time.sleep(10)
ok = chaifenExcel(file_to_save)
if ok == 'ok':
return 'ok'
else:
return 'no'
def chaifenExcel(excelfile):
print(excelfile)
savept = op.split(excelfile)[0] + "/"
data = pd.read_excel(excelfile)
rows = data.shape[0]
department_list = []
for i in range(rows):
temp = data["主管所"][i]
if temp not in department_list:
department_list.append(temp)
for department in department_list:
new_df = data[data["主管所"] == department]
new_df.to_excel(savept + str(department) + ".xlsx", sheet_name=department, index=False)
if __name__ == '__main__':
app.run(port=5001,debug=True,use_reloader=False)
Win7x86系统,Python3.8.10,Flask==2.2.3,pandas==1.5.3,requests==2.27.1,运行代码后会下载一个Excel文件,
程序运行到data = pd.read_excel(excelfile)就会报错zipfile. BadZipFile: File is not a zip file,
将程序中的chaifenExcel函数单独作为一个PY文件处理下载的Excel文件,就可以正常运行。
完全相同的代码环境,Win10x64也可以正常运行。 |