欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python?針對在子文件夾中的md文檔實現(xiàn)批量md轉(zhuǎn)word

 更新時間:2022年04月10日 18:15:11   作者:多好一次  
這篇文章主要介紹了python?針對在子文件夾中的md文檔實現(xiàn)批量md轉(zhuǎn)word,但是自己保存的md文檔在不同的文件夾,而大部分只能實現(xiàn)同一文件夾內(nèi)的轉(zhuǎn)換,得出下列總結(jié),需要的朋友可以參考一下

前言;

最近想要實現(xiàn)批量將mardown文檔轉(zhuǎn)化為word。網(wǎng)上有很多解決的方法,但是自己保存的md文檔在不同的文件夾,而大部分只能實現(xiàn)同一文件夾內(nèi)的轉(zhuǎ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)文章

最新評論