Python處理PDF及生成多層PDF實(shí)例代碼
Python提供了眾多的PDF支持庫(kù),本文是在Python3環(huán)境下,試用了兩個(gè)庫(kù)來(lái)完成PDF的生成的功能。PyPDF對(duì)于讀取PDF支持較好,但是沒(méi)找到生成多層PDF的方法。Reportlab看起來(lái)更成熟,能夠利用Canvas很方便的生成多層PDF,這樣就能夠?qū)崿F(xiàn)圖片掃描上來(lái)的內(nèi)容也可以進(jìn)行內(nèi)容搜索的目標(biāo)。
Reportlab
生成雙層PDF
雙層PDF應(yīng)用PDF中的Canvas概念,先畫(huà)文字,最后將圖片畫(huà)上去,這樣就是兩層的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)容有很多問(wèn)題,可以看這個(gè)問(wèn)題列表。文檔中也有說(shuō)明。
| 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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(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í)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
解決pycharm每次打開(kāi)項(xiàng)目都需要配置解釋器和安裝庫(kù)問(wèn)題
最近在使用pycharm開(kāi)發(fā)新項(xiàng)目的時(shí)候,每次打開(kāi)新的工程都顯示沒(méi)有解釋器,要不加了解釋器就是代碼一堆沒(méi)有紅色錯(cuò)誤提示沒(méi)有模塊問(wèn)題,很多朋友都遇到過(guò)這種情況,現(xiàn)小編把解決方法分享到腳本之家平臺(tái),需要的朋友一起看看吧2020-02-02
Python sklearn分類(lèi)決策樹(shù)方法詳解
決策樹(shù)(Decision Tree)是在已知各種情況發(fā)生概率的基礎(chǔ)上,通過(guò)構(gòu)成決策樹(shù)來(lái)求取凈現(xiàn)值的期望值大于等于零的概率,評(píng)價(jià)項(xiàng)目風(fēng)險(xiǎn),判斷其可行性的決策分析方法,是直觀運(yùn)用概率分析的一種圖解法2022-09-09
Python實(shí)現(xiàn)獲取域名所用服務(wù)器的真實(shí)IP
本文是給大家分享的使用python獲取到域名所在服務(wù)器的真實(shí)IP,原因是現(xiàn)在很多的網(wǎng)站都使用了CDN,大家很難直接查到域名的服務(wù)器的IP,本文是使用了一個(gè)巧妙的方法,詳情請(qǐng)仔細(xì)看看下文吧2015-10-10
Django在pycharm下修改默認(rèn)啟動(dòng)端口的方法
今天小編就為大家分享一篇Django在pycharm下修改默認(rèn)啟動(dòng)端口的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07

