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:01【Python 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 word、excel、ppt 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
The sidebar is recommended
- Writing sample code for functions in Python
- Summary of operation methods of Python set (about 20 operation methods), with sample code attached
- Python -- functions
- Anonymous and recursive functions in Python
- How to query the methods (member functions) of a class or an object in Python [using the function dir()]
- Summary of operation methods of Python Dictionary (dict) (about 18 operation methods), with sample code attached
- Collect hot search lists in Python at work, which can be called a fishing artifact
- Running Django and Vue on docker
- Data classification in pandas
- About Python: docxtpl is embedded by default when inserting pictures
guess what you like
How to solve the problem of CSV? (Language Python)
Installation and use of redis (Python)
Python implements sending mail (implements single / group mail verification code)
On the built-in object type of Python -- number (one of the differences between py2 and PY3)
Python God uses a problem to help you solve the problems of formal and actual parameters in Python functions
"Project Euler Python Mini tutorial" 001-010 solution introduction
Most beginners learn Python and web automation. In this way, they learn and give up
Python matrices and numpy arrays
Exciting challenge: Python crawler crawls the cover picture of station B
After installing python3, use the yum command to report an error?
Random recommended
- New features of python3.6, 3.7, 3.8 and 3.9
- Application of Python simplehttpserver
- Python sending mail (single / group) - yagmail module
- After learning these English words, mom doesn't have to worry that I can't learn Python any more
- 1-python+ selenium automated test (detailed tutorial) in the series of exercises of "teach you by hand"
- Cannot unmarshal array into go value of type main
- Analysis of the principle of Python import
- Python quickly saves pictures in wechat official account articles (multiple articles can be specified)
- Python error reporting series (14) -- selenium support for phantom JS has been deprecated
- Python variable data type
- Advanced Python Programming - functions and modules
- Python conditional judgment and loop statements
- Python dictionary nesting
- I want to use Python to write a census management software. I want to ask about the ideas and software involved
- I want to use Python to write a census management software. I want to consult the ideas and software involved.
- Python program has no idea
- How to set the initial position of the cursor in Python Tkinter
- The scrapy framework only gets a set of results. I don't know why (Language Python)
- Code problems in Python
- Python automation framework
- Vscode - offline extension installation tutorial (take Python plug-in installation as an example)
- _ What are the application scenarios in Python
- Python writing yaml file
- On the strange phenomenon of Python objects
- System learning Python -- unit test unittest: Test Report
- Learn Python in this way, and the boss licks back the resume in the trash can: 25K per month
- Guess the age of American mathematician Wiener
- Python machine learning day03
- Random seed()
- Programming science | you may be wrong about Python
- Is Python really worth learning
- What is the charm of python, which is warmly pursued by countless programmers?
- Python is popular for several reasons: These data tell you
- Picture to character drawing in Python, so easy!
- Data type conversion in pandas module
- Python Basics - (1) overview of Python
- Data Science Library Python -- learning of time series data
- Django project - error reporting
- [run the script framework in Django and store the data in the database]
- Complete Python exception handling in ten minutes