使用 Python 将 Word 文档转换为 HTML 或 Markdown

马哥Linux运维

共 1283字,需浏览 3分钟

 ·

2021-09-12 21:48



这篇简短的文章将指导您如何在基于 Python 的 CLI  Mammoth的帮助下,以简单的方式将 .docx word 文档转换为简单的网页文档 ( .html ) 或 Markdown 文档 ( .md ) 



据统计Statista调查(202016日),Microsoft Office套件是目前最流行的办公软件。您可以使用 Microsoft Word 轻松地做快速笔记、简短报告、教程文档等。而且,您可能希望将文档内容作为 Web 文档 ( .html )) 或 Markdown 文档 ( .md )与您的一些朋友、同事、客户共享。过去,在网络上托管一些网络文档可能会很昂贵,但现在云服务对于公共文档(例如GitHub Pages)来说非常便宜甚至免费。


Install Mammoth

确保PC 上安装了 Python 和 PIP。然后,打开 CMD 或终端并使用以下命令:

pip install mammoth


Docx 转换为HTML

使用命令行:

$ mammoth input_name.docx output_name.html


使用Python

import mammothwith open("sample.docx", "rb") as docx_file:    result = mammoth.convert_to_html(docx_file)with open("sample.html", "w") as html_file:    html_file.write(result.value)


Docx 转换为MD

使用命令行:

$ mammoth .\sample.docx output.md --output-format=markdown


使用Python

with open("sample.docx", "rb") as docx_file:    result = mammoth.convert_to_markdown(docx_file)with open("sample.md", "w") as markdown_file:    markdown_file.write(result.value)

文章转载:Python编程学习圈
(版权归原作者所有,侵删)

点击下方“阅读原文”查看更多

浏览 56
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报