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 use python to transfer Word to PDF in batches

2025-01-19 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.

Share To

Internet Technology

Wechat

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

12
Report