python?針對在子文件夾中的md文檔實現(xiàn)批量md轉(zhuǎn)word
前言;
最近想要實現(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)文章
Python socket實現(xiàn)的簡單通信功能示例
這篇文章主要介紹了Python socket實現(xiàn)的簡單通信功能,結(jié)合實例形式分析了Python socket通信的相關(guān)概念、原理、客戶端與服務器端實現(xiàn)技巧以及socketserver模塊多并發(fā)簡單實現(xiàn)方法,需要的朋友可以參考下2018-08-08Python 出現(xiàn)錯誤TypeError: ‘NoneType’ object is not iterable解決辦法
這篇文章主要介紹了Python 出現(xiàn)錯誤TypeError: ‘NoneType’ object is not iterable解決辦法的相關(guān)資料,需要的朋友可以參考下2017-01-01numpy和pandas中數(shù)組的合并、拉直和重塑實例
今天小編就為大家分享一篇numpy和pandas中數(shù)組的合并、拉直和重塑實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06解決phantomjs截圖失敗,phantom.exit位置的問題
今天小編就為大家分享一篇解決phantomjs截圖失敗,phantom.exit位置的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05