feat: 导出全部方案时自动包含Excel报表
This commit is contained in:
38
gui.py
38
gui.py
@@ -11,6 +11,7 @@ from main import (
|
|||||||
load_data_from_excel,
|
load_data_from_excel,
|
||||||
generate_wind_farm_data,
|
generate_wind_farm_data,
|
||||||
visualize_design,
|
visualize_design,
|
||||||
|
export_all_scenarios_to_excel,
|
||||||
)
|
)
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
@@ -229,11 +230,44 @@ def index():
|
|||||||
|
|
||||||
import zipfile
|
import zipfile
|
||||||
|
|
||||||
zip_filename = "all_designs.zip"
|
# 1. 确定文件名
|
||||||
|
zip_filename = "all_designs_result.zip"
|
||||||
|
excel_result_name = "wind_farm_design_result.xlsx"
|
||||||
|
|
||||||
|
# 推断 main.py 生成的原始 Excel 路径
|
||||||
|
generated_excel_path = "wind_farm_design.xlsx"
|
||||||
|
|
||||||
|
if state.get("original_filename"):
|
||||||
|
name_no_ext = os.path.splitext(state["original_filename"])[0]
|
||||||
|
zip_filename = f"{name_no_ext}_result.zip"
|
||||||
|
excel_result_name = f"{name_no_ext}_result.xlsx"
|
||||||
|
|
||||||
|
if state.get("excel_path"):
|
||||||
|
dir_name = os.path.dirname(state["excel_path"])
|
||||||
|
generated_excel_path = os.path.join(
|
||||||
|
dir_name, f"{name_no_ext}_design.xlsx"
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with zipfile.ZipFile(
|
with zipfile.ZipFile(
|
||||||
zip_filename, "w", zipfile.ZIP_DEFLATED
|
zip_filename, "w", zipfile.ZIP_DEFLATED
|
||||||
) as zipf:
|
) as zipf:
|
||||||
|
# 2. 添加 Excel 结果表
|
||||||
|
if os.path.exists(generated_excel_path):
|
||||||
|
zipf.write(generated_excel_path, arcname=excel_result_name)
|
||||||
|
else:
|
||||||
|
# 尝试重新生成
|
||||||
|
try:
|
||||||
|
temp_excel = "temp_export.xlsx"
|
||||||
|
export_all_scenarios_to_excel(
|
||||||
|
state["results"], temp_excel
|
||||||
|
)
|
||||||
|
zipf.write(temp_excel, arcname=excel_result_name)
|
||||||
|
os.remove(temp_excel)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"生成Excel失败: {e}")
|
||||||
|
|
||||||
|
# 3. 添加所有 DXF
|
||||||
for res in state["results"]:
|
for res in state["results"]:
|
||||||
safe_name = "".join(
|
safe_name = "".join(
|
||||||
[
|
[
|
||||||
@@ -255,7 +289,7 @@ def index():
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
ui.download(zip_filename)
|
ui.download(zip_filename)
|
||||||
ui.notify("已导出所有方案", type="positive")
|
ui.notify("已导出所有方案 (含Excel)", type="positive")
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
ui.notify(f"导出失败: {ex}", type="negative")
|
ui.notify(f"导出失败: {ex}", type="negative")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user