You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
from docx import Document
from docx . shared import Pt
from docx . oxml . ns import qn
import re
def markdown_to_docx ( markdown_text , output_file = " report.docx " ) :
"""
将 Markdown 格式的字符串转换为 Word 文档 (.docx)
参数:
markdown_text (str): Markdown 格式的字符串
output_file (str): 输出的 Word 文件名(默认 " report.docx " )
"""
# 初始化 Word 文档
doc = Document ( )
# 设置默认字体为宋体
doc . styles [ ' Normal ' ] . font . name = ' 宋体 '
doc . styles [ ' Normal ' ] . _element . rPr . rFonts . set ( qn ( ' w:eastAsia ' ) , ' 宋体 ' )
# 按行处理 Markdown 内容
for line in markdown_text . split ( " \n " ) :
# 处理标题(#、##、###)
if line . startswith ( " # " ) :
level = line . count ( " # " )
text = line . lstrip ( " # " ) . strip ( )
if level == 1 :
doc . add_heading ( text , level = 0 )
elif level == 2 :
doc . add_heading ( text , level = 1 )
elif level == 3 :
doc . add_heading ( text , level = 2 )
# 处理无序列表(- 或 *)
elif line . startswith ( " - " ) or line . startswith ( " * " ) :
text = line . lstrip ( " -* " ) . strip ( )
doc . add_paragraph ( text , style = ' List Bullet ' )
# 处理有序列表( 1. 或 2.)
elif re . match ( r " ^ \ d+ \ . " , line ) :
text = re . sub ( r " ^ \ d+ \ . " , " " , line ) . strip ( )
doc . add_paragraph ( text , style = ' List Number ' )
# 处理普通段落
else :
if line . strip ( ) : # 忽略空行
doc . add_paragraph ( line . strip ( ) )
# 保存 Word 文档
doc . save ( output_file )
print ( f " Word 文档已生成: { output_file } " )