Python processes PDF and generates multi-layer PDF instance code, pythonpdf

Source: Internet
Author: User

Python processes PDF and generates multi-layer PDF instance code, pythonpdf

Python provides a large number of PDF support libraries. This article uses two libraries in the Python3 environment to generate PDF files. PyPDF provides better support for reading PDF files, but does not find a method for generating multi-layer PDF files. Reportlab looks more mature and can use Canvas to easily generate multi-layer PDF files. This allows you to search for the content scanned by images.

Reportlab

Generate double-layer PDF

Double-layer PDF uses the Canvas concept in PDF to draw text first, and then draw the picture. This is a two-layer PDF. Import OS # import urllib2import timefrom reportlab import platypusfrom reportlab. lib. pagesizes import letterfrom reportlab. lib. units import inchfrom reportlab. platypus import SimpleDocTemplate, Imagefrom reportlab.pdf gen import canvasimage_file = ". /42.png" # Use Canvas to generate pdfc = canvas.Canvas('reportlab_canvas.pdf ', pagesize = letter) width, height = letterc. setFillColorRGB (0, 0.77, 0.77) # say Hello (note after rotate the y coord needs to be negative !) C. drawString (3 * inch, 3 * inch, "Hello World") c. drawImage (image_file, 0, 0) c. showPage () c. save ()

PyPDF2

Read PDF

from PyPDF2 import PdfFileWriter, PdfFileReaderoutput = PdfFileWriter()input1 = PdfFileReader(open("jquery.pdf", "rb"))# print document infoprint(input1.getDocumentInfo())# print how many pages input1 has:print ("pdf_document.pdf has %d pages." % input1.getNumPages())# print page contentpage_content = input1.getPage(0).extractText()print( page_content )# add page 1 from input1 to output document, unchangedoutput.addPage(input1.getPage(0))# add page 2 from input1, but rotated clockwise 90 degreesoutput.addPage(input1.getPage(1).rotateClockwise(90))# finally, write "output" to document-output.pdfoutputStream = open("PyPDF2-output.pdf", "wb")output.write(outputStream)

However, PyPDF has many problems in getting PDF content. You can refer to this list. This document also describes.

| extractText(self) | ## | # Locate all text drawing commands, in the order they are provided in the | # content stream, and extract the text. This works well for some PDF | # files, but poorly for others, depending on the generator used. This will | # be refined in the future. Do not rely on the order of text coming out of | # this function, as it will change if this function is made more | # sophisticated. | # | # Stability: Added in v1.7, will exist for all future v1.x releases. May | # be overhauled to provide more ordered text in the future. | # @return a unicode string object

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.