identity: author: your_username name: word_export label: en_US: Export to Word zh_Hans: 导出为Word文档description: human: en_US: Export conversation content to a Word document (.docx) zh_Hans: 将对话内容导出为Word文档(.docx) llm: > A tool that converts markdown text to a Word document (.docx) format. Use this tool when the user wants to save or export the conversation content as a Word document. The input text should be in markdown format.credential_schema: {} # No credentials neededtool_schema: markdown_content: type: string required: true label: en_US: Markdown Content zh_Hans: Markdown内容 human_description: en_US: The markdown content to convert to Word format zh_Hans: 要转换为Word格式的Markdown内容 document_name: type: string required: false label: en_US: Document Name zh_Hans: 文档名称 human_description: en_US: Name for the exported document (without extension) zh_Hans: 导出文档的名称(无需扩展名)
identity: author: your_username name: pdf_export label: en_US: Export to PDF zh_Hans: 导出为PDF文档description: human: en_US: Export conversation content to a PDF document zh_Hans: 将对话内容导出为PDF文档 llm: > A tool that converts markdown text to a PDF document. Use this tool when the user wants to save or export the conversation content as a PDF file. The input text should be in markdown format.credential_schema: {} # No credentials neededtool_schema: markdown_content: type: string required: true label: en_US: Markdown Content zh_Hans: Markdown内容 human_description: en_US: The markdown content to convert to PDF format zh_Hans: 要转换为PDF格式的Markdown内容 document_name: type: string required: false label: en_US: Document Name zh_Hans: 文档名称 human_description: en_US: Name for the exported document (without extension) zh_Hans: 导出文档的名称(无需扩展名)
import osimport tempfileimport uuidfrom docx import Documentfrom docx.shared import Ptfrom docx.enum.text import WD_PARAGRAPH_ALIGNMENTimport markdownfrom bs4 import BeautifulSoupdef convert_markdown_to_docx(markdown_text, document_name=None): """ Convert markdown text to a Word document and return the file path """ if not document_name: document_name = f"exported_document_{uuid.uuid4().hex[:8]}" # Convert markdown to HTML html = markdown.markdown(markdown_text) soup = BeautifulSoup(html, 'html.parser') # Create a new Word document doc = Document() # Process HTML elements and add to document for element in soup.find_all(['h1', 'h2', 'h3', 'h4', 'p', 'ul', 'ol']): if element.name == 'h1': heading = doc.add_heading(element.text.strip(), level=1) heading.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER elif element.name == 'h2': doc.add_heading(element.text.strip(), level=2) elif element.name == 'h3': doc.add_heading(element.text.strip(), level=3) elif element.name == 'h4': doc.add_heading(element.text.strip(), level=4) elif element.name == 'p': paragraph = doc.add_paragraph(element.text.strip()) elif element.name in ('ul', 'ol'): for li in element.find_all('li'): doc.add_paragraph(li.text.strip(), style='ListBullet') # Create temp directory if it doesn't exist temp_dir = tempfile.gettempdir() if not os.path.exists(temp_dir): os.makedirs(temp_dir) # Save the document file_path = os.path.join(temp_dir, f"{document_name}.docx") doc.save(file_path) return file_path
文档转换的核心挑战是保持格式和结构。此插件使用的方法首先将 markdown 转换为 HTML(一种中间格式),然后将该 HTML 处理为目标格式。这个两步过程提供了灵活性——你可以通过简单地添加与 HTML 表示配合工作的新输出模块来扩展它以支持其他格式。对于 PDF 生成,选择 WeasyPrint 是因为它提供具有 CSS 支持的高质量 PDF 渲染。对于 Word 文档,python-docx 提供对文档结构的精细控制。