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
参考资料:
1、PDF 1.0
2、PyPDF 2
3、PyPDF2 Homepage
4、PyPDF2 Documentation
5、python name 'file' is not defined的解决办法
6、ReportLab
7、用Python/reportlab生成PDF
8、Writing Pdf with Python: Add image
Python处理PDF及生成多层PDF的更多相关文章
- [Python]PDF合成小程序PDF合成小程序
运行平台:Python3.5 用刀了PyPDF2这个库,需要提前下载好. 源码如下: import PyPDF2, os #建立一个装pdf文件的数组pdfFiles = [] for fileNam ...
- 使用python把html网页转成pdf文件
我们看到一些比较写的比较好文章或者博客的时候,想保存下来到本地当一个pdf文件,当做自己的知识储备,以后即使这个博客或者文章的连接不存在了,或者被删掉,咱们自己也还有. 当然咱们作为一个coder,这 ...
- 「Python实用秘技04」为pdf文件批量添加文字水印
本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的系列文章「Python实用秘技」的第4期 ...
- java 如何在pdf中生成表格
1.目标 在pdf中生成一个可变表头的表格,并向其中填充数据.通过泛型动态的生成表头,通过反射动态获取实体类(我这里是User)的get方法动态获得数据,从而达到动态生成表格. 每天生成一个文件夹存储 ...
- [轉載]史上最强php生成pdf文件,html转pdf文件方法
之前有个客户需要把一些html页面生成pdf文件,然后我就找一些用php把html页面围成pdf文件的类.方法是可谓是找了很多很多,什么html2pdf,pdflib,FPDF这些都试过了,但是都没有 ...
- 史上最强php生成pdf文件,html转pdf文件方法
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- Python应用——自定义函数:分割PDF文件函数
案例 将一个 pdf 文件按要求分割为几个部分.比如说一个pdf有20页,分成5个pdf文件,每个pdf文件包含4页.设计函数实现? Python代码 from PyPDF2 import PdfFi ...
- Python代码教你批量将PDF转为Word
很多时候在学习时发现许多文档都是PDF格式,PDF格式却不利于学习使用,因此需要将PDF转换为Word文件,但或许你从网上下载了很多软件,但只能转换前五页(如WPS等),要不就是需要收费,那有没有免费 ...
- 根据PDF模板生成PDF文件(基于iTextSharp)
根据PDF模板生成PDF文件,这里主要借助iTextSharp工具来完成.场景是这样的,假如要做一个电子协议,用过通过在线填写表单数据,然后系统根据用户填写的数据,生成电子档的协议.原理很简单,但是每 ...
随机推荐
- Bad connection to FS. command aborted. exception: Call to chaoren/192.168.80.100:9000 failed on connection exception: java.net.ConnectException: Connection refused
Bad connection to FS. command aborted. exception: Call to chaoren/192.168.80.100:9000 failed on conn ...
- 牛客练习赛3 F - 监视任务
链接:https://www.nowcoder.net/acm/contest/13/F来源:牛客网 题目描述
- CSUOJ 1901 赏赐 OR 灾难 单调栈
Description 大G南征北战终于打下了大片土地成立了G国,大G在开国大典上传召帮助自己南征北战的三大开国元勋小A,小B,小C进殿,并要赏赐三人大量宝物以显示天恩浩荡.大G在征服其他国家的时候抢 ...
- 深度学习基础系列(十一)| Keras中图像增强技术详解
在深度学习中,数据短缺是我们经常面临的一个问题,虽然现在有不少公开数据集,但跟大公司掌握的海量数据集相比,数量上仍然偏少,而某些特定领域的数据采集更是非常困难.根据之前的学习可知,数据量少带来的最直接 ...
- JDK源码分析(二)——LinkedList
目录 LinkedList LinkedList继承结构 LinkedList内部类Node LinkedList成员属性 LinkedList构造方法 重要方法 Deque方法的实现 遍历 总结 L ...
- BoneBlack am335x can0 通讯配置与测试
准备工具: 1.内核3.14.65,u-boot.文件系统 2.boneblack开发板 3.串口线.电源线,测试线,测试夹 一.配置内核支持CAN通讯 [*] Networking support ...
- Java反射在Android中的使用
转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6606687.html 做Java开发或者安卓开发的同学经常都会遇到这样一个问题,如果我们想调用A类里面的一 ...
- touchscreem
<触摸屏种类> 常见 电阻式触摸屏 电容式触摸屏 不常见 红外线技术触摸屏 表面声波触摸屏 适量压力传感触摸屏 <原理分析> 电阻式触摸屏 如图所示,在屏幕的上下和左右加上一个 ...
- Ruby on rails配置环境问题【慢慢添加】
1, ruby -v 与 rvm list 显示不同的版本号 ,使用rvm use 2.2.3后,报如下错误: RVM is not a function 解决办法: $ source ~/.rv ...
- SecureCRT 如何改变对话框--每行输入字符数的多少
通过设置Logical columns的大小,选中Retain size an font来改变每行输入字符数的多少 1.设置Session Options–>Terminal–>Emula ...