-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
139 lines (116 loc) · 5.08 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from flask import Flask, render_template, request, jsonify, send_file
import os
import json
import shutil
import zipfile
from werkzeug.utils import secure_filename
from table_to_svg import TableToSVG
app = Flask(__name__)
# 配置
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['OUTPUT_FOLDER'] = 'output'
app.config['SCHEMES_FILE'] = 'config/schemes.json'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max-limit
# 确保必要的目录存在
for folder in ['uploads', 'output', 'config']:
os.makedirs(folder, exist_ok=True)
def load_schemes():
if os.path.exists(app.config['SCHEMES_FILE']):
with open(app.config['SCHEMES_FILE'], 'r', encoding='utf-8') as f:
return json.load(f)
return []
def save_schemes(schemes):
with open(app.config['SCHEMES_FILE'], 'w', encoding='utf-8') as f:
json.dump(schemes, f, ensure_ascii=False, indent=2)
@app.route('/')
def index():
schemes = load_schemes()
return render_template('index.html', schemes=schemes)
@app.route('/api/schemes', methods=['GET', 'POST'])
def handle_schemes():
if request.method == 'GET':
return jsonify(load_schemes())
if request.method == 'POST':
scheme = request.json
schemes = load_schemes()
schemes.append(scheme)
save_schemes(schemes)
return jsonify({"message": "方案保存成功"})
def process_file(input_path, output_dir, converter):
"""处理单个表格文件"""
filename = os.path.basename(input_path)
output_path = os.path.join(
output_dir,
f"{os.path.splitext(filename)[0]}.svg"
)
converter.create_svg(input_path, output_path)
@app.route('/api/convert', methods=['POST'])
def convert():
if 'file' not in request.files:
return jsonify({"error": "没有上传文件"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"error": "没有选择文件"}), 400
# 获取配置参数
config = json.loads(request.form.get('config'))
total_width = config['totalWidth']
row_height = config['rowHeight']
line_height = config['lineHeight']
column_widths = config['columnWidths']
# 新增配置参数
converter = TableToSVG()
converter.TOTAL_WIDTH = total_width
converter.MIN_CELL_HEIGHT = row_height
converter.LINE_HEIGHT = line_height
converter.FIXED_WIDTHS = column_widths
converter.ROW_SPACING = config.get('rowSpacing', 0)
converter.COLUMN_SPACING = config.get('columnSpacing', 0)
converter.ODD_ROW_COLOR = config.get('oddRowColor', '#ffffff')
converter.EVEN_ROW_COLOR = config.get('evenRowColor', '#f8f9fa')
converter.HEADER_FONT_SIZE = config.get('headerFontSize', 14)
converter.HEADER_FONT_WEIGHT = config.get('headerFontWeight', 'bold')
converter.CONTENT_FONT_SIZE = config.get('contentFontSize', 12)
converter.CONTENT_FONT_WEIGHT = config.get('contentFontWeight', 'normal')
converter.HEADER_ROW_COLOR = config.get('headerRowColor', '#f8f9fa')
converter.ODD_ROW_COLOR = config.get('oddRowColor', '#ffffff')
converter.EVEN_ROW_COLOR = config.get('evenRowColor', '#f8f9fa')
# 清理目录
for folder in [app.config['UPLOAD_FOLDER'], app.config['OUTPUT_FOLDER']]:
shutil.rmtree(folder, ignore_errors=True)
os.makedirs(folder)
# 处理上传的文件
filename = secure_filename(file.filename)
if filename.endswith('.zip'):
# 如果是zip文件,解压处理
zip_path = os.path.join(app.config['UPLOAD_FOLDER'], 'upload.zip')
file.save(zip_path)
# 解压文件
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(app.config['UPLOAD_FOLDER'])
# 处理所有表格文件
for root, dirs, files in os.walk(app.config['UPLOAD_FOLDER']):
for file in files:
if file.endswith(('.xlsx', '.xls', '.csv')):
input_path = os.path.join(root, file)
process_file(input_path, app.config['OUTPUT_FOLDER'], converter)
else:
# 如果是单个表格文件,直接处理
if not filename.endswith(('.xlsx', '.xls', '.csv')):
return jsonify({"error": "不支持的文件格式"}), 400
# 保存文件
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
# 处理文件
process_file(file_path, app.config['OUTPUT_FOLDER'], converter)
# 创建输出压缩包
output_zip = os.path.join(app.config['OUTPUT_FOLDER'], 'output.zip')
with zipfile.ZipFile(output_zip, 'w') as zipf:
for root, dirs, files in os.walk(app.config['OUTPUT_FOLDER']):
for file in files:
if file.endswith('.svg'):
file_path = os.path.join(root, file)
arcname = os.path.basename(file_path)
zipf.write(file_path, arcname)
return send_file(output_zip, as_attachment=True)
if __name__ == '__main__':
app.run(debug=True, port=0, host='0.0.0.0') # 添加 host='0.0.0.0' 使其能在局域网访问