当前位置: 首页 > 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')

http://www.lqws.cn/news/151957.html

相关文章:

  • 【react+antd+vite】优雅的引入svg和阿里巴巴图标
  • Java在word中指定位置插入图片。
  • npm run dev 报错:Error: error:0308010C:digital envelope routines::unsupported
  • Flash烧录速度和加载配置速度(纯FPGA ZYNQ)
  • 使用ReactNative加载Svga动画支持三端【Android/IOS/Harmony】
  • FPGA 的硬件结构
  • 70年使用权的IntelliJ IDEA Ultimate安装教程
  • android 之 Tombstone
  • SSH/RDP无法远程连接?腾讯云CVM及通用服务器连接失败原因与超全排查指南
  • Mysql 身份认证绕过漏洞 CVE-2012-2122
  • 如何利用Elastic Stack(ELK)进行安全日志分析
  • 关于easyexcel动态下拉选问题处理
  • 大模型微调技术全景图:从全量更新到参数高效适配
  • 三表查询SQL怎么写?----小白初学+案例引入
  • SQL SERVER中获取外部数据的两种方法!
  • Postgresql常用函数操作
  • Flask-SQLAlchemy使用小结
  • AppWeb
  • JavaSec-XSS
  • 如何防止看板任务长期停滞不前
  • Ubuntu中常用的网络命令指南
  • html文字红色粗体,闪烁渐变动画效果
  • 在Spring Boot 3.3中使用Druid数据源及其监控功能
  • 分析vban的utlis中的helper方法(1)——数组
  • Linux-07 ubuntu 的 chrome 启动不了
  • Ubuntu ifconfig 查不到ens33网卡
  • 【Android基础回顾】三:Android启动流程
  • 44、web实验-后台管理系统基本功能
  • CentOS 7 如何pip3安装pyaudio?
  • 【ArcGIS应用】ArcGIS‌应用如何进行影像分类?