Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize batch md to word by python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "python how to achieve batch md to word" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "python how to achieve batch md to word" article can help you solve the problem.

Preface

Recently, I want to convert mardown documents to word in bulk. There are many solutions on the Internet, but their own saved md documents in different folders, and most of them can only achieve the conversion within the same folder, so slightly improved to get the following functions.

From glob import globfrom pathlib import Pathimport osdirs = [d for d in glob (". / * * /")] # if used in this folder, adjust to the following code # dirs = [d for d in glob (". /")] # extract all md document paths 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) # batch convert all md documents to docxfor 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, "converted to", doc_path_2) except Exception as e: print (e)

To centralize the converted word documents into the same folder as the python program.

The code is as follows:

From glob import globfrom pathlib import Pathimport osdirs = [d for d in glob (". / * * /")] # if used in this folder, adjust to the following code # dirs = [d for d in glob (". /")] # extract all md document paths 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, "converted to" Doc_path_1) except Exception as e: print (e) this is the end of the introduction on "how to convert python from batch md to word". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report