日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python?針對在子文件夾中的md文檔實現批量md轉word_python

作者:多好一次 ? 更新時間: 2022-06-11 編程語言

前言;

最近想要實現批量將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)

原文鏈接:https://blog.csdn.net/weixin_42750611/article/details/123432881

欄目分類
最近更新