SoFunction
Updated on 2024-11-18

Python Office Automation will be any document into PDF format

First, word to PDF

Here with the help of Python docx2pdf to complete the conversion operation, the library's installation command is as follows:

pip install docx2pdf

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

Here Tatsuo new two word files as a demonstration, open one of the word to see

It contains not only text, but also images

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

Which word_path is a folder for storing word files, word_to_pdf is a folder for storing converted pdf.

Open the first pdf with the following content:

You can see the text, images, and typography **** These are all exactly the same as the original document (word).

Second, excel to PDF

The library you need to use here is comtypes, and the following is a straightforward example.

Above word to pdf has taught you to learn to read all the files from the folder, all the same here without further ado.

pip install pywin32

Objective: To convert excel file to PDF

Here Tatsu creates a new excel file as a demonstration.

import os
from  import DispatchEx
excel_path = "D:/Public/0626/Python Researchers.xls"
pdf_path = "D:/Public/0626/Python Researcher.pdf."

xlApp = DispatchEx("")
 = False
 = 0
books = (excel_path,False)
(0, pdf_path)
(False)
()

Run after the generation of pdf files

Open pdf

You can see that the data in excel have all been converted to PDF format.

Third, ppt to PDF

The library you need to use here is comtypes, and the following is a straightforward example.

Above word to pdf has taught you to learn to read all the files from the folder, all the same here without further ado.

Goal: ppt to pdf

This is a ppt that Tatsuko made when he did the sharing before, let's take this ppt as an example.

import 
import os
def ppt_to_pdf():
    #Set the path
    input_file_path=("Python Learning Planning Route.pptx")
    output_file_path=("Python Learning Planning Route.pdf.")
    #Create PDF
    powerpoint=("")
    =1
    slides=(input_file_path)
    #Save PDF
    (output_file_path,32)
    ()

Here will be ppt: python learning planning route.pptx to python learning planning route.pdf

Open the pdf and its contents are as follows:

IV. Summary

This article basically on the successful realization of the goal requirements, from the results are still very good! The complete source code can be assembled from the code in the article (all have been shared in the article), interested readers can try it themselves!

to this article on how to teach you how to use Python to automatically convert any file to PDF format of the article is introduced to this, more relevant Python file to PDF content, please search for my previous articles or continue to browse the following articles I hope that you will support me in the future!