使用 Python 將 Word 文檔轉(zhuǎn)換為 HTML 或 Markdown


這篇簡(jiǎn)短的文章將指導(dǎo)您如何在基于 Python 的 CLI — Mammoth的幫助下,以簡(jiǎn)單的方式將 .docx word 文檔轉(zhuǎn)換為簡(jiǎn)單的網(wǎng)頁(yè)文檔 ( .html ) 或 Markdown 文檔 ( .md ) 。
據(jù)統(tǒng)計(jì)Statista調(diào)查(2020年1月6日),Microsoft Office套件是目前最流行的辦公軟件。您可以使用 Microsoft Word 輕松地做快速筆記、簡(jiǎn)短報(bào)告、教程文檔等。而且,您可能希望將文檔內(nèi)容作為 Web 文檔 ( .html )) 或 Markdown 文檔 ( .md )與您的一些朋友、同事、客戶共享。過(guò)去,在網(wǎng)絡(luò)上托管一些網(wǎng)絡(luò)文檔可能會(huì)很昂貴,但現(xiàn)在云服務(wù)對(duì)于公共文檔(例如GitHub Pages)來(lái)說(shuō)非常便宜甚至免費(fèi)。
Install Mammoth
確保PC 上安裝了 Python 和 PIP。然后,打開 CMD 或終端并使用以下命令:
pip install mammoth將Docx 轉(zhuǎn)換為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 轉(zhuǎn)換為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)
文章轉(zhuǎn)載:Python編程學(xué)習(xí)圈
(版權(quán)歸原作者所有,侵刪)
![]()

點(diǎn)擊下方“閱讀原文”查看更多
評(píng)論
圖片
表情
