当前位置: 首页 > news >正文

Python实现markdown文件转word

1.markdown内容如下:

2.转换后的内容如下:

3.附上代码:

import argparse
import os
from markdown import markdown
from bs4 import BeautifulSoup
from docx import Document
from docx.shared import Inches
from docx.enum.text import WD_PARAGRAPH_ALIGNMENTdef convert_md_to_docx(input_file, output_file=None):# 若未指定输出文件,从输入文件路径推断if not output_file:base_name, _ = os.path.splitext(input_file)output_file = f"{base_name}.docx"# 读取 Markdown 文件内容try:with open(input_file, 'r', encoding='utf-8') as f:md_content = f.read()except FileNotFoundError:print(f"错误:找不到文件 '{input_file}'")returnexcept Exception as e:print(f"错误:读取文件时出错 '{input_file}': {e}")return# 将 Markdown 转换为 HTMLhtml_content = markdown(md_content, extensions=['markdown.extensions.fenced_code','markdown.extensions.tables','markdown.extensions.nl2br'])# 解析 HTML 内容soup = BeautifulSoup(html_content, 'html.parser')# 创建 Word 文档doc = Document()# 处理 HTML 内容并添加到 Word 文档process_soup_elements(soup, doc)# 保存 Word 文档try:doc.save(output_file)print(f"成功:已将 Markdown 文件 '{input_file}' 转换为 Word 文档 '{output_file}'")except Exception as e:print(f"错误:保存文件时出错 '{output_file}': {e}")def process_soup_elements(soup, doc):"""处理 BeautifulSoup 对象中的所有元素"""# 如果 soup 直接包含内容(没有 html/body 标签)if soup.name is None or soup.name != 'html':for element in soup.children:process_element(element, doc)else:# 处理标准的 html 结构for element in soup.children:if element.name == 'html':for html_child in element.children:if html_child.name == 'body':for body_child in html_child.children:process_element(body_child, doc)elif html_child.name == 'head':# 通常忽略 head 部分,但可以根据需要处理passelse:# 处理不在 body 中的元素process_element(html_child, doc)else:process_element(element, doc)def process_element(element, doc):"""处理单个 HTML 元素并添加到 Word 文档"""if element.name is None:# 处理文本节点if element.strip():doc.add_paragraph(element.strip())returnif element.name == 'h1':# 处理一级标题doc.add_heading(element.get_text(), level=1)elif element.name == 'h2':# 处理二级标题doc.add_heading(element.get_text(), level=2)elif element.name == 'h3':# 处理三级标题doc.add_heading(element.get_text(), level=3)elif element.name == 'p':# 处理段落p = doc.add_paragraph()for child in element.children:if child.name is None:p.add_run(str(child))elif child.name == 'strong':p.add_run(child.get_text()).bold = Trueelif child.name == 'em':p.add_run(child.get_text()).italic = Trueelif child.name == 'code':p.add_run(child.get_text()).font.name = 'Courier New'elif child.name == 'a':p.add_run(child.get_text())elif element.name == 'ul':# 处理无序列表for li in element.find_all('li'):doc.add_paragraph(li.get_text(), style='List Bullet')elif element.name == 'ol':# 处理有序列表for li in element.find_all('li'):doc.add_paragraph(li.get_text(), style='List Number')elif element.name == 'pre':# 处理代码块if element.code:code_text = element.code.get_text()p = doc.add_paragraph()p.add_run(code_text).font.name = 'Courier New'elif element.name == 'table':# 处理表格table = doc.add_table(rows=1, cols=len(element.find('tr').find_all(['th', 'td'])))hdr_cells = table.rows[0].cells# 添加表头for i, th in enumerate(element.find('tr').find_all('th')):hdr_cells[i].text = th.get_text()# 添加表格内容for row in element.find_all('tr')[1:]:row_cells = table.add_row().cellsfor i, td in enumerate(row.find_all('td')):row_cells[i].text = td.get_text()elif element.name == 'img':# 处理图片img_src = element.get('src')if img_src and os.path.exists(img_src):try:doc.add_picture(img_src, width=Inches(5.0))last_paragraph = doc.paragraphs[-1]last_paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTERexcept Exception as e:print(f"警告:无法添加图片 '{img_src}': {e}")if __name__ == "__main__":convert_md_to_docx('E:\work\\tempProject\pythonProject\zhuan\\123.md')

相关文章:

  • 六、数据库的安全性
  • 书籍将正方形矩阵顺时针转动90°(8)0605
  • RADIUS-如何获取凭证
  • [蓝桥杯]植树
  • LLaMA.cpp 文本生成惩罚机制详解:从原理到实践的完整指南
  • Spring Bean生命周期全解析:从创建到销毁的底层细节
  • Docker MCP 目录和工具包简介:使用 MCP 为 AI 代理提供支持的简单安全方法
  • Flask+LayUI开发手记(八):通用封面缩略图上传实现
  • 复变函数中的对数函数及其MATLAB演示
  • 深入理解React Hooks的原理与实践
  • RDMA简介5之RoCE v2队列
  • 【高等数学】傅里叶级数逼近例子
  • 将 Jupyter Notebook 的默认存储路径从 C 盘迁移到 D 盘,可以通过以下步骤实现:
  • [密码学实战]彻底理解位(bit)与字节(byte)在十六进制处理中的区别
  • 【EN 18031】访问控制机制(ACM - 3):儿童玩具的防护盾
  • vue:当前对象添加对应值
  • Python cryptography【密码库】库功能与使用指南
  • 【Redis】类型补充
  • 墨者学院-密码学实训隐写术第二题
  • [闭源saas选项]Pinecone:为向量数据库而生的实时语义搜索引擎
  • 如何更换wordpress域名/咸阳seo公司
  • 安徽六安特产/搜索引擎优化是指什么
  • 外贸网站建设哪家比较好/百度手机极速版
  • 电子商务网站建设需要什么/广告公司推广平台
  • 深圳宝安沙井网站建设/中国网络营销公司
  • 九江网站推广/企业网站推广的形式有