使用Python将Word文档转换为HTML或Markdown
文章最后更新时间:2021年09月14日已超过290天没有更新。
如何将Word文档转换为HTML或Markdown呢?我们可以使用Python的库Mammoth 来完成转换操作
环境准备
Pyton官网下载地址 :https://www.python.org/downloads/
这边使用:python-3.8.6-amd64.exe
https://www.python.org/ftp/python/3.8.6/python-3.8.6-amd64.exe
安装Python3.8.6
1、勾选Add Python 3.8 to PATH加入系统变量,接着点击Customize installation(自定义安装)
2、可写安装包,默认全选,点击Next进入下一步
3、指定Python安装路径,这边安装在C:\Python36,点击Install 开始安装
4、安装中,等待安装完成
5、安装完成后,点击close关闭
安装Mammoth库
1、使用Windows+R键(开始菜单找到运行窗口),输入cmd调出命令窗口
2、在DOS窗口里输入,如为加入变量则需进入Python安装目录下的Scripts文件夹内然后运行pip
pip install mammoth
等到提示下载成功就可以了
3、将Docx 转换为HTML
本教程操作目录为C:\ahaoyw
使用命令行
mammoth input_name.docx output_name.html
使用Python代码
import mammoth with open("input_name.docx", "rb") as docx_file: result = mammoth.convert_to_html(docx_file) with open("output_name.html", "w") as html_file: html_file.write(result.value)
4、将Docx 转换为MD
使用命令行
mammoth input_name.docx output.md --output-format=markdown
使用Python代码
import mammoth with open("input_name.docx", "rb") as docx_file: result = mammoth.convert_to_markdown(docx_file) with open("output.md", "w") as markdown_file: markdown_file.write(result.value)
若文章图片、下载链接等信息出错,请联系反馈,博主将第一时间更新!如果喜欢本站,请打赏支持本站,谢谢!
文章版权声明:除非注明,否则均为阿豪运维笔记原创文章,转载或复制请以超链接形式并注明出处。