This is the 25th day of my participation in Gwen Challenge

Pain point: I believe everyone will encounter a scenario. The teacher/boss asks you to convert a document to PDF in batches (more than one, one can be done manually), and it’s boring, unskilled and tiring.

Imagine if I put these files in a folder, execute the program, and the files will be ready in a few minutes. So half a day’s work, just spend a few minutes to solve. Isn’t it delightful!!

Today, Brother Chen will teach you to batch convert any document to PDF. Here, take word, Excel, PPT for example, these three formats of documents to PDF.

01, Word to PDF

To do this, use Python’s docx2pdf library, which is installed as follows:

pip install docx2pdf
Copy the code

Objective: Read all word files under the folder, then convert, and finally save to the corresponding folder.

Here Chen brother create two word files as a demonstration, open one of the word to see

It contains not only text, but also pictures


import os
from docx2pdf import convert
word_path = 'word_path'
word_to_pdf = 'word_to_pdf'
for i,j,name in os.walk(word_path):
    for word_name in name:
         convert(word_path+"/"+word_name, word_to_pdf+"/"+word_name.replace("docx"."pdf"))
Copy the code

Word_path is the folder for storing Word files, word_to_pdf is the converted PDF folder.

Open the first PDF, which reads as follows:

You can see that the text, images, and typesetting **** are exactly the same as the original document (Word).

02. Excel to PDF

The library you need to use here is comtypes, so let’s go straight to an example.

The above word to PDF has taught you how to read all the files from the folder, so the same here will not be repeated.

pip install pywin32
Copy the code

Goal: Convert Excel files to PDF

Here I’ll create a new Excel file as a demonstration


import os
from win32com.client import DispatchEx
excel_path = "D:/ public /0626/Python researcher.xls"
pdf_path = "D:/ public id /0626/Python researcher.pdf"

xlApp = DispatchEx("Excel.Application")
xlApp.Visible = False
xlApp.DisplayAlerts = 0
books = xlApp.Workbooks.Open(excel_path,False)
books.ExportAsFixedFormat(0, pdf_path)
books.Close(False)
xlApp.Quit()
Copy the code

Generate PDF file after run

Open the PDF

You can see that all the data in Excel has been converted to PDF format.

03, PPT to PDF

The library you need to use here is comtypes, so let’s go straight to an example.

The above word to PDF has taught you how to read all the files from the folder, so the same here will not be repeated.

Goal: PPT to PDF

This is a PPT that Brother Chen used to share. Let’s take this PPT as an example



import comtypes.client
import os
def ppt_to_pdf() :
    # set path
    input_file_path=os.path.abspath("Python learning route planning. PPTX")
    output_file_path=os.path.abspath("Path planning for Python learning.pdf")
    # to create PDF
    powerpoint=comtypes.client.CreateObject("Powerpoint.Application")
    powerpoint.Visible=1
    slides=powerpoint.Presentations.Open(input_file_path)
    # save a PDF
    slides.SaveAs(output_file_path,32)
    slides.Close()
Copy the code

Here will be PPT: Python learning Planning path. PPTX to Python learning planning path. PDF

Open the PDF as follows:

04, subtotal

This paper is basically successful to achieve the goal requirements, from the effect is still very good! Complete source code can be combined by the text (has been all shared in the text), interested readers can try their own!

Do try it! Do try it! Do try it!