python?針對在子文件夾中的md文檔實現(xiàn)批量md轉(zhuǎn)word
前言;
最近想要實現(xiàn)批量將mardown文檔轉(zhuǎn)化為word。網(wǎng)上有很多解決的方法,但是自己保存的md文檔在不同的文件夾,而大部分只能實現(xiàn)同一文件夾內(nèi)的轉(zhuǎn)換,因此稍加改進(jìn),得出以下功能。



from glob import glob
from pathlib import Path
import os
dirs = [ d for d in glob("./**/")]
# 用在本文件夾內(nèi)則調(diào)整為下列代碼
# 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)
? ? ? ??
# 批量轉(zhuǎn)化所有的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,"已經(jīng)轉(zhuǎn)化為",doc_path_2)
? ? except Exception as e:
? ? ? ? print(e)若要將轉(zhuǎn)化的word文檔集中到python程序所在文件夾內(nèi)。
代碼如下:
from glob import glob
from pathlib import Path
import os
dirs = [d for d in glob("./**/")]
# 用在本文件夾內(nèi)則調(diào)整為下列代碼
# 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,"已經(jīng)轉(zhuǎn)化為",doc_path_1)
? ? ? ? except Exception as e:
? ? ? ? ? ? print(e)到此這篇關(guān)于python 針對在子文件夾中的md文檔實現(xiàn)批量md轉(zhuǎn)word的文章就介紹到這了,更多相關(guān)python 批量md轉(zhuǎn)word內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python HTTP下載文件并顯示下載進(jìn)度條功能的實現(xiàn)
這篇文章主要介紹了Python HTTP下載文件并顯示下載進(jìn)度條功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Python深度學(xué)習(xí)pytorch神經(jīng)網(wǎng)絡(luò)圖像卷積運算詳解
這篇文章主要介紹了Python深度學(xué)習(xí)關(guān)于pytorch神經(jīng)網(wǎng)絡(luò)圖像卷積的運算示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
從np.random.normal()到正態(tài)分布的擬合操作
這篇文章主要介紹了從np.random.normal()到正態(tài)分布的擬合操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
pytorch1.0中torch.nn.Conv2d用法詳解
今天小編就為大家分享一篇pytorch1.0中torch.nn.Conv2d用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

