后端flask部分
from flask import Flask, render_template, request, redirect, url_for,send_file
from flask import send_from_directory
from werkzeug.utils import secure_filename
import os
# global files
#####################################################################
#密钥处理部分
key=get_key()
#####################################################################
############################################################################
#服务器后端
############################################################################
app = Flask(__name__)
@app.route('/')
def main():#主函数
return "<a href='http://127.0.0.1:8088/auth/'>点击认证</a>"
@app.route('/auth/<auth_key>')#密钥认证与跳转
def auth(auth_key):
if auth_key==key:
return redirect('http://127.0.0.1:8088/upload')
else:
return "Invalid password"
@app.route('/upload', methods=['POST', 'GET'])#文件上传#可以将认证内嵌在upload中
def upload():
# global files
if request.method == 'POST':
f = request.files['file']
basepath = os.path.dirname(__file__)
# files=f.filename
upload_path = os.path.join(basepath, 'static/uploads', f.filename)
f.save(upload_path)
print('uploading ...')
return render_template('upload.html')
@app.route('/download/<filename>')#处理结束的下载
def download(filename):
# global files
file_path = 'C:/Users/Administrator/Desktop/pyflask/static/uploads/' + filename
response = send_file(file_path, as_attachment=True)
response.headers['Content-Disposition'] = 'attachment; filename={}'.format(filename)
return response
app.run(port='8088')
文档处理函数(调用Word COM接口,实测WPS兼容)
#####################################################################
#文件处理函数部分
#####################################################################
def format_document(input_file, output_file):
# 启动 Word 应用
word = win32.Dispatch('Word.Application')
word.Visible = False # 让 Word 后台运行
# 打开文档
doc = word.Documents.Open(input_file)
# 设置页边距
doc.PageSetup.TopMargin = 3.7 * 28.35 # 3.7 cm 转换为磅
doc.PageSetup.BottomMargin = 3.5 * 28.35 # 3.5 cm 转换为磅
doc.PageSetup.LeftMargin = 2.8 * 28.35 # 2.8 cm 转换为磅
doc.PageSetup.RightMargin = 2.6 * 28.35 # 2.6 cm 转换为磅
# 获取正文的第一段(标题)并设置字体
title_para = doc.Paragraphs(1)
title_para.Range.Font.Name = '方正小标宋简体' # 标题字体
title_para.Range.Font.Size = 22 # 2号字
title_para.Range.Font.Bold = False # 加粗
title_para.Alignment = 1 # 居中对齐
# 在标题和正文之间空一行
doc.Paragraphs.Add()
# 设置正文的字体(从第二段开始)
for para in doc.Paragraphs:
if para.Range.Start > title_para.Range.End: # 跳过标题
para.Range.Font.Name = '仿宋_GB2312' # 正文字体
para.Range.Font.Size = 16 # 3号字
para.Range.Font.Bold = False # 正文不加粗
para.Alignment = 0 # 左对齐
# 在最后一行前插入两个空行
doc.Paragraphs.Add()
doc.Paragraphs.Add()
# 获取当前日期并设置格式
current_date = datetime.now().strftime('%Y年%m月%d日')
# 设置日期行
date_para = doc.Paragraphs.Add()
date_para.Range.Text = current_date
date_para.Alignment = 2 # 右对齐
# 设置日期的字体
date_para.Range.Font.Name = '仿宋_GB2312' # 文字部分字体
date_para.Range.Font.Size = 16 # 字号16pt
# 设置数字部分字体为Times New Roman
# 将日期分为文字和数字两部分
date_run = date_para.Range.Duplicate
date_run.Find.Execute('%Y年%m月') # 查找年月部分
date_run.Font.Name = 'Times New Roman'
date_run.Font.Size = 16 # 字号16pt
# 保存文件
doc.SaveAs(output_file)
doc.Close() # 关闭文档
word.Quit() # 退出 Word 应用
print(f"文档格式化完成,已保存为 {output_file}")
用到的templates模板页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>upload:</h1>
<form action="" enctype='multipart/form-data' method='POST'>
<input type="file" name="file">
<input type="submit" value="upload">
</form>
</body>
</html>
问题
后端处理部分应对多线程并发时候会出现问题(解决中🤷♂️)
极丑UI😒
服务器怎么装Word??🤔