模型生成内容后,还需要拷贝粘贴到word文件中,能否按照知识库中或者上传的word文件格式,自动格式化后生成word文件返回。
可以部署 Pandoc 服务,然后在函数中使用 Pandoc 进行格式转换。
Pandoc 文档:Pandoc - Pandoc User’s Guide
参考函数:
import requests
def generate_file(content, input_format="markdown", output_format="docx"):
host_ip = "123.xx.xx.2" # 替换为实际IP地址
service_url = f"http://{host_ip}:30060/convert"
response = requests.post(service_url, json={
"content": content,
"format": input_format,
"output_format": output_format
})
if response.ok:
result = response.json()
file_id = result["file_id"]
fmt = result["format"]
download_link = f"http://{host_ip}:30060/download/{file_id}.{fmt}"
return download_link
else:
raise Exception(f"生成失败: {response.text}")
能按照知识库或上传文件中的格式,生成一样格式的word文件吗
Pandoc 可以设置文档模板的。