In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use python to transfer Word to PDF in batches". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to use python to transfer Word to PDF in batches"!
The realization of the conversion function needs to use the third-party library comtypes, and the installation can use pip install comtypes. The basic idea of the implementation is: we put the Word files that need to be converted in a directory, and realize the traversal and conversion of the files through Python.
The code implementation of the conversion function is also relatively simple, as follows:
Def get_file (input_path, output_path):
# get a list of all file names
Filename_list = os.listdir (input_path)
# get a list of all Word file names
Wordname_list = [filename for filename in filename_list\
If filename.endswith ((".doc", ".docx"))]
For wordname in wordname_list:
# separate Word file names and suffixes and convert them to PDF names
Pdfname = os.path.splitext (wordname) [0] + ".pdf"
# if the PDF file corresponding to the current Word file exists, it will not be converted
If pdfname in filename_list:
Continue
# stitching path and file name
Wordpath = os.path.join (input_path, wordname)
Pdfpath = os.path.join (output_path, pdfname)
# Generator
Yield wordpath, pdfpath
Def word2pdf (input_path, output_path):
Word = comtypes.client.CreateObject ("Word.Application")
Word.Visible = 0
For wordpath, pdfpath in get_file (input_path, output_path):
Newpdf = word.Documents.Open (wordpath)
Newpdf.SaveAs (pdfpath, FileFormat=17)
Newpdf.Close ()
Let's take a look at the effect. The contents of the Word file are as follows:
Take a look at the converted PDF file:
We can see that whether it is the text style or the picture, the conversion effect is better.
At this point, I believe you have a deeper understanding of "how to use python to transfer Word to PDF in batches". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.