This article describes how to convert python images to pdf files in detail, which has some reference value, interested friends can refer to this article for details about the python image-to-pdf method, which has some reference value. interested friends can refer
Import OS
Import sys
From reportlab. lib. pagesizes import A4, landscape
From reportlab.pdf gen import canvas
'''
Traverse all jpg files in the current directory and merge them into pdf files according to the folder name
Python 3.4.4
Image files are named by numbers in order
'''
Def conpdf ():
# Obtain the horizontal A4 size
(W, h) = landscape (A4)
# Traverse the current directory
For root, dirs, files in OS. walk (OS. getcwd ()):
# Create a pdf file based on the root directory name
C = canvas. Canvas (OS. path. basename (root) + ". pdf", pagesize = landscape (A4 ))
# Print (OS. path. basename (root) + ". pdf ")
# Used to store jpg files
Jpg_list = []
# Extract the jpg file from the file list and put it in the list
For p in files:
# Save the jpg file name to the list
If p [-4:] then else '.jpg ':
# Jpg_list.append (root + "\" + p)
Jpg_list.append (p)
# Sort file names
Jpg_list.sort (key = lambda x: int (x [:-4])
# Print (jpg_list)
For f in jpg_list:
# Draw images to the canvas in sequence
C. drawImage (root + "\" + f, 0, 0, w, h)
# End the current page and create a new page
C. showPage ()
C. save ()
Print ("OK .")
Conpdf ()
The above describes how to convert python images to pdf. For more information, see other related articles in the first PHP community!