国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2019-11-25 16:13:47
字體:
來源:轉載
供稿:網友

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

Reportlab

生成雙層PDF

雙層PDF應用PDF中的Canvas概念,先畫文字,最后將圖片畫上去,這樣就是兩層的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.pdfgen 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

讀取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)

但是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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 明光市| 木兰县| 墨江| 陕西省| 巴林右旗| 彰武县| 郑州市| 库尔勒市| 荣成市| 自治县| 蓬莱市| 新宾| 竹北市| 左贡县| 揭东县| 上栗县| 郑州市| 雷波县| 抚顺县| 崇左市| 正定县| 大庆市| 梧州市| 岚皋县| 涿州市| 宝山区| 新昌县| 青州市| 林口县| 西乌| 武鸣县| 瑞安市| 巴彦县| 梁河县| 克东县| 仪陇县| 石泉县| 仁怀市| 陵水| 会理县| 泰和县|