Home >Backend Development >Python Tutorial >Introduction to the method of converting python images to pdf

Introduction to the method of converting python images to pdf

高洛峰
高洛峰Original
2017-03-26 17:48:122586browse

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

                                                                                                                                                                                                                          to pagesize = landscape(A4))  

#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)

for f in jpg_list: On canvas

                                                                                                                                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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn