前言;
最近想要实现批量将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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
查看更多关于python 针对在子文件夹中的md文档实现批量md转word的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did92969