欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python處理PDF及生成多層PDF實例代碼

 更新時間:2017年04月24日 16:45:44   作者:Cocowool  
Python提供了眾多的PDF支持庫,本篇文章主要介紹了Python處理PDF及生成多層PDF實例代碼,這樣就能夠?qū)崿F(xiàn)圖片掃描上來的內(nèi)容也可以進行內(nèi)容搜索的目標

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

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)容有很多問題,可以看這個問題列表。文檔中也有說明。

| 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 redis存入字典序列化存儲教程

    python redis存入字典序列化存儲教程

    這篇文章主要介紹了python redis存入字典序列化存儲教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 使用python-Jenkins批量創(chuàng)建及修改jobs操作

    使用python-Jenkins批量創(chuàng)建及修改jobs操作

    這篇文章主要介紹了使用python-Jenkins批量創(chuàng)建及修改jobs操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • Django之提交表單與前后端交互的方法

    Django之提交表單與前后端交互的方法

    今天小編就為大家分享一篇Django之提交表單與前后端交互的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python使用phoenixdb操作hbase的方法示例

    python使用phoenixdb操作hbase的方法示例

    這篇文章主要介紹了python使用phoenixdb操作hbase的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • 老生常談python之鴨子類和多態(tài)

    老生常談python之鴨子類和多態(tài)

    下面小編就為大家?guī)硪黄仙U刾ython之鴨子類和多態(tài)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Pygame實戰(zhàn)之檢測按鍵正確的小游戲

    Pygame實戰(zhàn)之檢測按鍵正確的小游戲

    這篇文章主要為大家介紹了利用Pygame模塊實現(xiàn)的檢測按鍵正確的小游戲:每個字母有10秒的按鍵時間,如果按對,則隨機產(chǎn)生新的字符,一共60s,如果時間到了,則游戲結(jié)束??靵砀S小編一起學(xué)習(xí)一下吧
    2021-12-12
  • Python中使用實現(xiàn)輸出哈沙德數(shù)的多種方法小結(jié)

    Python中使用實現(xiàn)輸出哈沙德數(shù)的多種方法小結(jié)

    哈沙德數(shù)(Harshad?Number),又稱Niven數(shù),是指一個自然數(shù),它可以被它的各位數(shù)字之和整除,本文將探討如何使用多種不同的方法來判斷一個數(shù)字是否是哈沙德數(shù),感興趣的可以了解下
    2024-01-01
  • 基于python opencv單目相機標定的示例代碼

    基于python opencv單目相機標定的示例代碼

    這篇文章主要介紹了基于python opencv單目相機標定的實現(xiàn)代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • Python編程新標準學(xué)會十項好習(xí)慣提升編碼質(zhì)量

    Python編程新標準學(xué)會十項好習(xí)慣提升編碼質(zhì)量

    這篇文章主要為大家介紹了Python編程新標準學(xué)會十項好習(xí)慣提升編碼質(zhì)量,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • Python生產(chǎn)者與消費者模型中的優(yōu)勢介紹

    Python生產(chǎn)者與消費者模型中的優(yōu)勢介紹

    這篇文章主要介紹了python多進程中的生產(chǎn)者和消費者模型優(yōu)勢,生產(chǎn)者是指生產(chǎn)數(shù)據(jù)的任務(wù),消費者是指消費數(shù)據(jù)的任務(wù)。當(dāng)生產(chǎn)者的生產(chǎn)能力遠大于消費者的消費能力,生產(chǎn)者就需要等消費者消費完才能繼續(xù)生產(chǎn)新的數(shù)據(jù)
    2023-03-03

最新評論