current position:Home>Office weapon! Quickly convert any file to PDF in Python

Office weapon! Quickly convert any file to PDF in Python

2022-06-24 08:10:01Python researcher

Pain points : I believe everyone will come across a kind of scene . teacher / The boss asked you to A file is converted to pdf, And it's a group ( More than one , One can be done manually ), And it's boring work , There is no technical content and tired .

Just imagine , If I put these documents Put it in a folder , Carry out the procedure , In a few minutes, the files will be ready . Such a half day's work , as long as Take a few minutes to solve 了 . Isn't it beautiful !!!

Today, brother Chen teaches you to convert any file into PDF, It's based on daily work wordexcelppt For example , These three formats are converted to PDF.

01、word turn PDF

With the help of Python Of docx2pdf To complete the conversion operation , The installation command of the library is as follows :

pip install docx2pdf

The goal is : Read everything in the folder word file , And then switch , Finally, save it to the corresponding folder .

Here are two new ones word Document as a demonstration , Open one of them word have a look

It's not just words , It also contains 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"))

among word_path It's for storage. word File folder ,word_to_pdf It's converted pdf Storage folder .

Open the first one pdf, The contents are as follows :

You can see written words picture 、 as well as Typesetting ** these All the same as the original document (word) As like as two peas **.

02、excel turn PDF

The library you need to use here is comtypes, Let's go straight to the case .

above word turn pdf I've taught you how to read all the files from the folder , All the same here will not be repeated .

pip install pywin32

The goal is : take excel The file to PDF

Here is a new one excel Document as a demonstration

import os
from win32com.client import DispatchEx
excel_path = "D:/ official account /0626/Python researcher .xls"
pdf_path = "D:/ official account /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()

After running, generate pdf file

open pdf

You can see excel All the data in has been converted to PDF Format .

03、ppt turn PDF

The library you need to use here is comtypes, Let's go straight to the case .

above word turn pdf I've taught you how to read all the files from the folder , All the same here will not be repeated .

The goal is :ppt To pdf

This one was made by brother Chen when he was sharing ppt, Let's take this ppt For example

import comtypes.client
import os
def ppt_to_pdf():
    # Set the path 
    input_file_path=os.path.abspath("Python Learn to plan your route .pptx")
    output_file_path=os.path.abspath("Python Learn to plan your route .pdf")
    # establish PDF
    powerpoint=comtypes.client.CreateObject("Powerpoint.Application")
    powerpoint.Visible=1
    slides=powerpoint.Presentations.Open(input_file_path)
    # preservation PDF
    slides.SaveAs(output_file_path,32)
    slides.Close()

It will be here ppt:Python Learn to plan your route .pptx To Python Learn to plan your route .pdf

open pdf It reads as follows :

04、 Summary

In this paper, we have achieved the goal successfully , From the effect point of view, it is very good ! The complete source code can be composed of the code in the article ( All of them have been shared in the article ), Interested readers can try it on their own !

must do Try it must do Try it must do Try it !

copyright notice
author[Python researcher],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/175/20210628105202659a.html

Random recommended