Home >Backend Development >Python Tutorial >Introduction to the method of converting python images to pdf
This article mainly introduces the method of converting python images to pdf in detail. It has certain reference value. Interested friends can refer to it
import os
import sys
from reportlab.lib.pagesizes import A4, landscape
from reportlab.pdfgen import canvas
'''
Traverse all jpg files in the current directory and merge them into pdf documents according to the folder names
python 3.4.4
Picture files are named in order with numbers
'''
def conpdf():
#Get the horizontal A4 size
(w, h) = landscape(A4)
#Traverse the current directory
for root,dirs,files in os.walk(os.getcwd ()):
#Create a pdf based on the root directory name
#print(os.path.basename(root)+".pdf") #Used to store jpg files
jpg_
list=[] #Retrieve jpg files from the file list and put them into the list
for p in files:
#Save jpg file names into the list
if
p[-4:] =='.jpg': ’ s ’ s ‐ off ‐ ‐ ‐ ‐ ‐ #jpg_list.append(root + "\ \" +p)
jpg_list.
sort(
key##) #= lambda x: int (x [: -4]) #Print (jpg_list) c. showPage() c.save() print ("ok.")conpdf()The above is the detailed content of Introduction to the method of converting python images to pdf. For more information, please follow other related articles on the PHP Chinese website!