How to convert python images to pdf

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.