日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python处理pdf 层_Python处理PDF及生成多层PDF实例代码

發布時間:2024/9/27 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python处理pdf 层_Python处理PDF及生成多层PDF实例代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python提供了眾多的PDF支持庫,本文是在Python3環境下,試用了兩個庫來完成PDF的生成的功能。PyPDF對于讀取PDF支持較好,但是沒找到生成多層PDF的方法。Reportlab看起來更成熟,能夠利用Canvas很方便的生成多層PDF,這樣就能夠實現圖片掃描上來的內容也可以進行內容搜索的目標。

Reportlab

生成雙層PDF

雙層PDF應用PDF中的Canvas概念,先畫文字,最后將圖片畫上去,這樣就是兩層的PDF。

import os

# import urllib2

import time

from reportlab import platypus

from reportlab.lib.pagesizes import letter

from reportlab.lib.units import inch

from reportlab.platypus import SimpleDocTemplate, Image

from reportlab.pdfgen import canvas

image_file = "./42.png"

# Use Canvas to generate pdf

c = canvas.Canvas('reportlab_canvas.pdf', pagesize=letter)

width, height = letter

c.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

讀取PDF

from PyPDF2 import PdfFileWriter, PdfFileReader

output = PdfFileWriter()

input1 = PdfFileReader(open("jquery.pdf", "rb"))

# print document info

print(input1.getDocumentInfo())

# print how many pages input1 has:

print ("pdf_document.pdf has %d pages." % input1.getNumPages())

# print page content

page_content = input1.getPage(0).extractText()

print( page_content )

# add page 1 from input1 to output document, unchanged

output.addPage(input1.getPage(0))

# add page 2 from input1, but rotated clockwise 90 degrees

output.addPage(input1.getPage(1).rotateClockwise(90))

# finally, write "output" to document-output.pdf

outputStream = open("PyPDF2-output.pdf", "wb")

output.write(outputStream)

但是PyPDF獲取PDF內容有很多問題,可以看這個問題列表。文檔中也有說明。

| 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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。

時間: 2017-04-21

總結

以上是生活随笔為你收集整理的python处理pdf 层_Python处理PDF及生成多层PDF实例代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。