main
HuangHai 3 weeks ago
parent 9dee2dbc10
commit 15efa4cb1e

@ -87,14 +87,14 @@ async def save_to_word(request: Request):
logger.error(f"Request parsing failed: {str(e)}")
raise HTTPException(status_code=400, detail=f"Invalid request: {str(e)}")
# 创建临时HTML文件
temp_html = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex + ".html")
with open(temp_html, "w", encoding="utf-8") as f:
# 创建临时Markdown文件
temp_md = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex + ".md")
with open(temp_md, "w", encoding="utf-8") as f:
f.write(html_content)
# 使用pandoc转换
output_file = os.path.join(tempfile.gettempdir(), "【理想大模型】问答.docx")
subprocess.run(['pandoc', temp_html, '-o', output_file,'--resource-path=static'], check=True)
subprocess.run(['pandoc', temp_md, '-o', output_file,'--resource-path=static'], check=True)
# 读取生成的Word文件
with open(output_file, "rb") as f:
@ -115,8 +115,8 @@ async def save_to_word(request: Request):
finally:
# 清理临时文件
try:
if temp_html and os.path.exists(temp_html):
os.remove(temp_html)
if temp_md and os.path.exists(temp_md):
os.remove(temp_md)
if output_file and os.path.exists(output_file):
os.remove(output_file)
except Exception as e:

Loading…
Cancel
Save