Python 官方文档:入门教程 => 点击学习
前言; 最近想要实现批量将mardown文档转化为Word。网上有很多解决的方法,但是自己保存的md文档在不同的文件夹,而大部分只能实现同一文件夹内的转换,因此稍加改进,得出以下功能
前言;
最近想要实现批量将mardown文档转化为Word。网上有很多解决的方法,但是自己保存的md文档在不同的文件夹,而大部分只能实现同一文件夹内的转换,因此稍加改进,得出以下功能。
from glob import glob
from pathlib import Path
import os
dirs = [ d for d in glob(".")]
# 用在本文件夹内则调整为下列代码
# dirs = [ d for d in glob("./")]
# 提取所有的md文档路径
al1_file_pathes=[]
for dir in dirs:
file_list=Path(dir).glob("*.md")
for file in file_list:
al1_file_pathes.append(".\\"+str(file))
print(file)
# 批量转化所有的md文档为docx
for md_path in al1_file_pathes:
doc_path=md_path.replace(".md",".docx")
command_new="pandoc -s "+md_path+" -o "+doc_path
print(command_new)
try:
res=os.popen(command_new).readlines()
if len(res)==0:
print(md_path,"已经转化为",doc_path_2)
except Exception as e:
print(e)
若要将转化的word文档集中到python程序所在文件夹内。
代码如下:
from glob import glob
from pathlib import Path
import os
dirs = [d for d in glob(".")]
# 用在本文件夹内则调整为下列代码
# dirs = [ d for d in glob("./")]
# 提取所有的md文档路径
for dir in dirs:
file_list = Path(dir).glob("*.md")
for file in file_list:
md_path = ".\\" + str(file)
doc_path_1 = os.path.split(file)[1].replace(".md", ".docx")
command_new_1 = "pandoc -s "+md_path+" -o "+doc_path_1
try:
res=os.popen(command_new_1).readlines()
if len(res)==0:
print(md_path,"已经转化为",doc_path_1)
except Exception as e:
print(e)
到此这篇关于Python 针对在子文件夹中的md文档实现批量md转word的文章就介绍到这了,更多相关python 批量md转word内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: python 针对在子文件夹中的md文档实现批量md转word
本文链接: https://www.lsjlt.com/news/117203.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0