點擊關注「Python數據分析實例」
設為「置頂或星標」,送達乾貨不錯過!
最近有一個開發需求,將生成的word數據報表以網頁格式推送,正好找到一個簡單快速轉換的模塊mammoth。
這篇簡短的文章將指導您如何在基於 Python 的 CLI — Mammoth的幫助下,以簡單的方式將.docx word 文檔轉換為簡單的網頁文檔 ( .html ) 或 Markdown 文檔 ( .md ) 。
據統計Statista調查(2020年1月6日),Microsoft Office套件是目前最流行的辦公軟體。您可以使用 Microsoft Word 輕鬆地做快速筆記、簡短報告、教程文檔等。而且,您可能希望將文檔內容作為 Web 文檔 ( .html )) 或 Markdown 文檔 ( .md )與您的一些朋友、同事、客戶共享。過去,在網絡上託管一些網絡文檔可能會很昂貴,但現在雲服務對於公共文檔(例如GitHub Pages)來說非常便宜甚至免費。
Install Mammoth
確保PC 上安裝了 Python 和 PIP。然後,打開 CMD 或終端並使用以下命令:
將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編程學習圈
感謝你的分享,點讚,在看三連