網站首頁 編程語言 正文
前言;
最近想要實現批量將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-08-15 Spring框架常考知識點總結
- 2022-06-17 一文輕松了解ASP.NET與ASP.NET?Core多環境配置對比_實用技巧
- 2022-08-27 一文讀懂Android?Kotlin的數據流_Android
- 2022-06-25 C++虛函數表的原理與使用解析_C 語言
- 2022-01-23 win7下與Virtualbox下的ubuntu11.04共享文件夾
- 2022-10-23 C++進程的創建和進程ID標識詳細介紹_C 語言
- 2022-05-24 redis中的bitmap你了解嗎_Redis
- 2022-10-17 Qt中TCP協議通信詳解_C 語言
- 最近更新
-
- 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同步修改后的遠程分支