Python處理PDF及生成多層PDF實(shí)例代碼
Python提供了眾多的PDF支持庫,本文是在Python3環(huán)境下,試用了兩個(gè)庫來完成PDF的生成的功能。PyPDF對于讀取PDF支持較好,但是沒找到生成多層PDF的方法。Reportlab看起來更成熟,能夠利用Canvas很方便的生成多層PDF,這樣就能夠?qū)崿F(xiàn)圖片掃描上來的內(nèi)容也可以進(jìn)行內(nèi)容搜索的目標(biāo)。
Reportlab
生成雙層PDF
雙層PDF應(yīng)用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內(nèi)容有很多問題,可以看這個(gè)問題列表。文檔中也有說明。
| 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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python實(shí)現(xiàn)橋接模式的代碼詳解
橋接模式是一種結(jié)構(gòu)型設(shè)計(jì)模式,它將抽象部分與其實(shí)現(xiàn)部分分離,使它們都可以獨(dú)立地變化,本文將給大家介紹如何使用Python實(shí)現(xiàn)橋接模式,需要的朋友可以參考下2024-02-02
opencv銀行卡號(hào)識(shí)別的項(xiàng)目實(shí)踐
本文主要介紹了opencv銀行卡號(hào)識(shí)別的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
解決pycharm每次打開項(xiàng)目都需要配置解釋器和安裝庫問題
最近在使用pycharm開發(fā)新項(xiàng)目的時(shí)候,每次打開新的工程都顯示沒有解釋器,要不加了解釋器就是代碼一堆沒有紅色錯(cuò)誤提示沒有模塊問題,很多朋友都遇到過這種情況,現(xiàn)小編把解決方法分享到腳本之家平臺(tái),需要的朋友一起看看吧2020-02-02
Python實(shí)現(xiàn)獲取域名所用服務(wù)器的真實(shí)IP
本文是給大家分享的使用python獲取到域名所在服務(wù)器的真實(shí)IP,原因是現(xiàn)在很多的網(wǎng)站都使用了CDN,大家很難直接查到域名的服務(wù)器的IP,本文是使用了一個(gè)巧妙的方法,詳情請仔細(xì)看看下文吧2015-10-10
Django在pycharm下修改默認(rèn)啟動(dòng)端口的方法
今天小編就為大家分享一篇Django在pycharm下修改默認(rèn)啟動(dòng)端口的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07

