Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize batch md to word by python

Shulou Source: shulou.com Published: 2022-06-01 00:09:48 09月21日 Update

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.

Tags: Documents files folders code knowledge differences methods industries paths adjustments utility content preface functions most practicality practicality location articles more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Huawei Xiaomi vpn Microsoft