Skip to main content

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 图表渲染方式:

  1. mermaid-py(推荐):功能丰富的Mermaid渲染库(版本0.7.0兼容性较好)
  2. Kroki API:在线渲染服务,无需本地依赖

渲染引擎会自动按上述顺序尝试,直到成功渲染。

Debugging Code Block Issues

If you encounter issues with code blocks, you can use these debugging features:

  1. Enable debug mode:

    md-to-docx input.md -o output.docx -d
    

    This will show detailed information about code block extraction and processing.

  2. 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:

  1. Special characters in the code that affect the regex pattern matching
  2. Improper code block formatting (e.g., missing newlines)
  3. Incorrect handling of line breaks in the code

Mermaid Diagrams Not Rendering

If Mermaid diagrams aren't rendering correctly, it might be due to:

  1. Mermaid CLI not being installed
  2. Network connection issues preventing access to the Kroki API
  3. Syntax errors in the Mermaid diagram code
  4. 库的版本不兼容(推荐使用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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

md_to_docx-0.1.6.tar.gz (30.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

md_to_docx-0.1.6-py3-none-any.whl (29.5 kB view details)

Uploaded Python 3

File details

Details for the file md_to_docx-0.1.6.tar.gz.

File metadata

  • Download URL: md_to_docx-0.1.6.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

Hashes for md_to_docx-0.1.6.tar.gz
Algorithm Hash digest
SHA256 52fe927dd31fe7b8b3eee9e564e46f670598b2a024855e2372728df14e68ab52
MD5 99644204a473cf3eab324db5bfe0f6eb
BLAKE2b-256 ed15ef3c359250ac4e1e3d441a90fe8fe947527948909af6e8f72e727c15600a

See more details on using hashes here.

File details

Details for the file md_to_docx-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: md_to_docx-0.1.6-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

Hashes for md_to_docx-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 2505aa48aa551c832307b504061db8a94bd0dc44239a5121472d9a7e5a926a73
MD5 2ba9bc842d47e199a7ae985ad1fa40d3
BLAKE2b-256 f0dcb350b1de33632c033f96fd46bd2b82759c3d2f9a16db32b64fd3bcf214a7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page