前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Python将Word文档转换为HTML或Markdown

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

原创
作者头像
用户1094633
发布2022-02-15 09:26:31
3.2K0
发布2022-02-15 09:26:31
举报
文章被收录于专栏:运维随笔

如何将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.png
image.png

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

image.png
image.png

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

image.png
image.png

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

image.png
image.png

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

image.png
image.png

安装Mammoth库

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

image.png
image.png

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

Python

代码语言:javascript
复制
pip install mammoth

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

image.png
image.png

3、将Docx 转换为HTML

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

使用命令行

Python

代码语言:javascript
复制
 mammoth input_name.docx output_name.html
image.png
image.png

使用Python代码

Python

代码语言:javascript
复制
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

代码语言:javascript
复制
mammoth input_name.docx output.md --output-format=markdown
image.png
image.png

使用Python代码

Python

代码语言:javascript
复制
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)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境准备
  • 安装Python3.8.6
  • 安装Mammoth库
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档