经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
python?针对在子文件夹中的md文档实现批量md转word
来源:jb51  时间:2022/4/11 8:45:16  对本文有异议

前言;

最近想要实现批量将mardown文档转化为word。网上有很多解决的方法,但是自己保存的md文档在不同的文件夹,而大部分只能实现同一文件夹内的转换,因此稍加改进,得出以下功能。

  1. from glob import glob
  2. from pathlib import Path
  3. import os
  4.  
  5. dirs = [ d for d in glob("./**/")]
  6.  
  7. # 用在本文件夹内则调整为下列代码
  8. # dirs = [ d for d in glob("./")]
  9.  
  10. # 提取所有的md文档路径
  11. al1_file_pathes=[]
  12. for dir in dirs:
  13. ? ? file_list=Path(dir).glob("*.md")
  14. ? ? for file in file_list:
  15. ? ? ? ? al1_file_pathes.append(".\\"+str(file))
  16. ? ? ? ? print(file)
  17.  
  18. ? ? ? ??
  19. # 批量转化所有的md文档为docx
  20. for md_path in al1_file_pathes:
  21. ? ? doc_path=md_path.replace(".md",".docx")
  22. ? ? command_new="pandoc -s "+md_path+" -o "+doc_path?
  23. ? ? print(command_new)
  24. ? ? try:
  25. ? ? ? ? res=os.popen(command_new).readlines()
  26. ? ? ? ? if len(res)==0:
  27. ? ? ? ? ? ? print(md_path,"已经转化为",doc_path_2)
  28. ? ? except Exception as e:
  29. ? ? ? ? print(e)

若要将转化的word文档集中到python程序所在文件夹内。

代码如下:

  1. from glob import glob
  2. from pathlib import Path
  3. import os
  4.  
  5. dirs = [d for d in glob("./**/")]
  6.  
  7. # 用在本文件夹内则调整为下列代码
  8. # dirs = [ d for d in glob("./")]
  9.  
  10. # 提取所有的md文档路径
  11. for dir in dirs:
  12. ? ? file_list = Path(dir).glob("*.md")
  13. ? ? for file in file_list:
  14. ? ? ? ? md_path = ".\\" + str(file)
  15. ? ? ? ? doc_path_1 = os.path.split(file)[1].replace(".md", ".docx")
  16. ? ? ? ? command_new_1 = "pandoc -s "+md_path+" -o "+doc_path_1
  17. ? ? ? ? try:
  18. ? ? ? ? ? ? res=os.popen(command_new_1).readlines()
  19. ? ? ? ? ? ? if len(res)==0:
  20. ? ? ? ? ? ? ? ? print(md_path,"已经转化为",doc_path_1)
  21. ? ? ? ? except Exception as e:
  22. ? ? ? ? ? ? print(e)

到此这篇关于python 针对在子文件夹中的md文档实现批量md转word的文章就介绍到这了,更多相关python 批量md转word内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号