網站首頁 編程語言 正文
前言;
最近想要實現批量將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
相關推薦
- 2022-06-08 淺談C#多線程下的調優_C#教程
- 2022-07-27 C++中this指針理解及作用_C 語言
- 2022-09-18 centos+nginx+uwsgi部署django項目上線_python
- 2023-02-27 python定時任務sched庫用法簡單實例_python
- 2022-08-02 源碼解析python中randint函數的效率缺陷_python
- 2023-04-03 GoLang中拼接字符串性能優化方法詳解_Golang
- 2022-10-30 Django視圖層與模板層實例詳解_python
- 2022-02-26 Echarts - 更改圖表圖例(legend)自定義顏色(并與數據段顏色對應)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支