当前位置:首页 > 编程笔记 > 正文
已解决

Word docx转html和markdown

来自网友在路上 152852提问 提问时间:2023-10-26 05:16:23阅读次数: 52

最佳答案 问答题库528位专家为你答疑解惑

Pypandoc使用pandoc来进行各种文本格式的转换。

安装

# 不带pandoc执行库
pip install pypandoc# 自带pandoc
pip install pypandoc_binary

使用

import pypandoc# convert all markdown files in a chapters/ subdirectory.
pypandoc.convert_file('chapters/*.md', 'docx', outputfile="somefile.docx")# convert all markdown files in the book1 and book2 directories.
pypandoc.convert_file(['book1/*.md', 'book2/*.md'], 'docx', outputfile="somefile.docx")# convert the front from another drive, and all markdown files in the chapter directory.
pypandoc.convert_file(['D:/book_front.md', 'book2/*.md'], 'docx', outputfile="somefile.docx")

支持pathlib

import pypandoc
from pathlib import Path# single file
input = Path('somefile.md')
output = input.with_suffix('.docx')
pypandoc.convert_file(input, 'docx', outputfile=output)# convert all markdown files in a chapters/ subdirectory.
pypandoc.convert_file(Path('chapters').glob('*.md'), 'docx', outputfile="somefile.docx")# convert all markdown files in the book1 and book2 directories.
pypandoc.convert_file([*Path('book1').glob('*.md'), *Path('book2').glob('*.md')], 'docx', outputfile="somefile.docx")
# pathlib globs must be unpacked if they are inside lists.

参考

https://pypi.org/project/pypandoc/
https://pandoc.org/help.html
https://www.strerr.com/cn/word2html.html
https://zhuanlan.zhihu.com/p/30891168
https://pypi.org/project/pandoc/

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"Word docx转html和markdown":http://eshow365.cn/6-24809-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!