Convert Markdown to DOCX with support for Mermaid diagrams
Project description
Markdown to DOCX Converter
一个功能强大的Markdown转DOCX文档转换器,支持Mermaid图表渲染。
特性
- 将Markdown文件转换为Word DOCX格式
- 自动渲染Mermaid图表为图像
- 支持代码块格式化和语法高亮
- 支持表格、列表和其他Markdown元素
安装
pip install md-to-docx
可选依赖
为了增强Mermaid图表渲染功能,可以安装以下可选依赖:
# 安装mermaid-py - 功能丰富的Mermaid渲染库
pip install mermaid-py==0.7.0 # 推荐安装此特定版本以避免兼容性问题
# 或者安装pymermaid - 另一个纯Python的Mermaid渲染选项
pip install pymermaid
如果没有安装这些可选依赖,系统将使用Kroki API在线渲染Mermaid图表。
使用方法
from md_to_docx import md_to_docx
# 读取 Markdown 文件
with open('your_file.md', 'r', encoding='utf-8') as f:
md_content = f.read()
# 转换为 DOCX
md_to_docx(md_content, output_file='output.docx')
Mermaid 图表渲染方式
本工具支持多种 Mermaid 图表渲染方式:
- mermaid-py(推荐):功能丰富的Mermaid渲染库(版本0.7.0兼容性较好)
- Kroki API:在线渲染服务,无需本地依赖
渲染引擎会自动按上述顺序尝试,直到成功渲染。
Debugging Code Block Issues
If you encounter issues with code blocks, you can use these debugging features:
-
Enable debug mode:
md-to-docx input.md -o output.docx -d
This will show detailed information about code block extraction and processing.
-
Use as a library with debug mode:
from md_to_docx import md_to_docx result = md_to_docx(markdown_content, "output.docx", debug_mode=True)
Common Issues
Code Blocks Not Displaying Correctly
If code blocks aren't displaying correctly, it might be due to:
- Special characters in the code that affect the regex pattern matching
- Improper code block formatting (e.g., missing newlines)
- Incorrect handling of line breaks in the code
Mermaid Diagrams Not Rendering
If Mermaid diagrams aren't rendering correctly, it might be due to:
- Mermaid CLI not being installed
- Network connection issues preventing access to the Kroki API
- Syntax errors in the Mermaid diagram code
- 库的版本不兼容(推荐使用mermaid-py==0.7.0)
Advanced Usage
Customizing Code Block Formatting
You can modify the format_code_block function in the source code to customize code block formatting, such as changing fonts, sizes, or border styles.
Adding New Markdown Extensions
You can add new Markdown extensions to support additional Markdown features by modifying the md_to_docx function:
from md_to_docx import md_to_docx
from md_to_docx.core import format_code_block
# Your custom implementation here
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file md_to_docx-0.1.4.tar.gz.
File metadata
- Download URL: md_to_docx-0.1.4.tar.gz
- Upload date:
- Size: 30.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa3a02aabf66a5959c59b1c821feca54c49eb9b6e0bcbb96f02896151c9c530a
|
|
| MD5 |
261e274347eca97c29fe368597ab8728
|
|
| BLAKE2b-256 |
20155f2403d84db97dbed86b3b7bda540c9d305480d1062f580d43082b63d4c4
|
File details
Details for the file md_to_docx-0.1.4-py3-none-any.whl.
File metadata
- Download URL: md_to_docx-0.1.4-py3-none-any.whl
- Upload date:
- Size: 29.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e35c5dacbe628485406f85751f99a222c08cbef9d748be99e38edab8540f05b1
|
|
| MD5 |
05b290f562bdee35e97f04b69a806212
|
|
| BLAKE2b-256 |
4ab74e5d9eb69e33cd41a8c7c019dbf4b5912745d91cff43f57cd0aa8c88bd31
|