社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

马哥Linux运维 • 2 年前 • 260 次点击  



这篇简短的文章将指导您如何在基于 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编程学习圈
(版权归原作者所有,侵删)

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

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/119890
 
260 次点击