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

您所在的位置:网站首页 怎么将word转化为html 使用Python将Word文档转换为HTML或Markdown

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

2023-10-07 15:57| 来源: 网络整理| 查看: 265

如何将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(自定义安装)

image.pngimage.png

2、可写安装包,默认全选,点击Next进入下一步

image.pngimage.png

3、指定Python安装路径,这边安装在C:\Python36,点击Install 开始安装

image.pngimage.png

4、安装中,等待安装完成

image.pngimage.png

5、安装完成后,点击close关闭

image.pngimage.png安装Mammoth库

1、使用Windows+R键(开始菜单找到运行窗口),输入cmd调出命令窗口

image.pngimage.png

2、在DOS窗口里输入,如为加入变量则需进入Python安装目录下的Scripts文件夹内然后运行pip

Python

pip install mammoth

等到提示下载成功就可以了

image.pngimage.png

3、将Docx 转换为HTML

本教程操作目录为C:\ahaoyw

使用命令行

Python

mammoth input_name.docx output_name.htmlimage.pngimage.png

使用Python代码

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

使用命令行

Python

mammoth input_name.docx output.md --output-format=markdownimage.pngimage.png

使用Python代码

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)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3