###################################处理PDF和Word文档###################################

'''

PDF和Word文档是二进制文件,除了文本之外,

它们还保存了许多字体、颜色和布局信息

'''

'''

从PDF提取文本

'''

###################################从PDF提取文本###################################

import PyPDF2

pdfFileObj=open(r'C:\Users\Administrator\Desktop\test.pdf','rb')

pdfReader=PyPDF2.PdfFileReader(pdfFileObj)

pdfReader.numPages

pageObj=pdfReader.getPage(0)

pageObj.extractText()

###################################解压PDF#########################################

import PyPDF2

pdfReader=PyPDF2.PdfFileReader(open(r'C:\Users\Administrator\Desktop\test.pdf','rb'))

pdfReader.isEncrypted    ####是否加密

pdfReader.getPage(0)

pdfReader.decrypt('rosebud')   ####提供解密口令

pageObj=pdfReader.getPage(0)

###################################创建PDF#########################################

'''

PyPDF2不能将任意文本写入PDF:

PyPDF2写入PDF的能力,仅限于从其他PDF中拷贝页面、旋转页面、重叠页面和加密文件

'''

'''

一般方式:

1、打开一个或多个已用的PDF(源PDF),得到PdfFileReader对象

2、创建一个新的PdfFileWriter对象

3、将页面从PdfFileReader对象拷贝到PdfFileWriter对象中

4、利用PdfFileWriter对象写入输出的PDF

'''

#####################################################拷贝页面###########################################################

def merge(pdf_one, pdf_two, filename='my.pdf',output_dir=r'C:\Users\Administrator\Desktop'):

input_one = file(pdf_one, 'rb')

input_two = file(pdf_two, 'rb')

pdf_input_one = PyPDF2.PdfFileReader(input_one)

pdf_input_two = PyPDF2.PdfFileReader(input_two)

numOne = pdf_input_one.getNumPages()

numTwo = pdf_input_two.getNumPages()

print numOne, numTwo

pdf_output = PyPDF2.PdfFileWriter()

for pageNum in range(numOne):

print 'hereo'

pageObj=pdf_input_one.getPage(pageNum)

pdf_output.addPage(pageObj)

for pageNum in range(numTwo):

print 'heret'

pageObj=pdf_input_two.getPage(pageNum)

pdf_output.addPage(pageObj)

pdf_name = output_dir+filename

print pdf_name

output_stream = file( pdf_name,'wb')

pdf_output.write(output_stream)

output_stream.close()

input_one.close()

input_two.close()

print 'Done!'

merge(r'C:\Users\Administrator\Desktop\Pairs_Trading_Quantitative Methods and Analysis.pdf',r'C:\Users\Administrator\Desktop\deMontjoye.SM.pdf')

#####################################################旋转页面###########################################################

'''

利用rotateClockwise()和rotateCounterClockwise()方法

PDF文档的页面也可以旋转90度的整数倍,向这些方法传入

整数90、180或270

'''

def merge(pdf_one, pdf_two, filename='my.pdf',output_dir=r'C:\Users\Administrator\Desktop'):

input_one = file(pdf_one, 'rb')

input_two = file(pdf_two, 'rb')

pdf_input_one = PyPDF2.PdfFileReader(input_one)

pdf_input_two = PyPDF2.PdfFileReader(input_two)

numOne = pdf_input_one.getNumPages()

numTwo = pdf_input_two.getNumPages()

print numOne, numTwo

pdf_output = PyPDF2.PdfFileWriter()

for pageNum in range(numOne):

print 'hereo'

pageObj=pdf_input_one.getPage(pageNum)

pageObj=pageObj.rotateClockwise(90)

pdf_output.addPage(pageObj)

for pageNum in range(numTwo):

print 'heret'

pageObj=pdf_input_two.getPage(pageNum)

pageObj=pageObj.rotateClockwise(90)

pdf_output.addPage(pageObj)

pdf_name = output_dir+filename

print pdf_name

output_stream = file( pdf_name,'wb')

pdf_output.write(output_stream)

output_stream.close()

input_one.close()

input_two.close()

print 'Done!'

merge(r'C:\Users\Administrator\Desktop\Pairs_Trading_Quantitative Methods and Analysis.pdf',r'C:\Users\Administrator\Desktop\deMontjoye.SM.pdf')

#####################################################叠加页面###########################################################

import PyPDF2

minutesFile=open(r'C:\Users\Administrator\Desktop\Pairs_Trading_Quantitative Methods and Analysis.pdf','rb')

pdfReader=PyPDF2.PdfFileReader(minutesFile)

minutesFirstPage=pdfReader.getPage(0)

pdfWatermarkReader=PyPDF2.PdfFileReader(open(r'C:\Users\Administrator\Desktop\deMontjoye.SM.pdf','rb'))

minutesFirstPage.mergePage(pdfWatermarkReader.getPage(0))

pdfWriter=PyPDF2.PdfFileWriter()

pdfWriter.addPage(minutesFirstPage)

for pageNum in range(1,pdfReader.numPages):

pageObj=pdfReader.getPage(pageNum)

pdfWriter.addPage(pageObj)

resultPdfFile=open(r'C:\Users\Administrator\Desktop\merge.pdf','wb')

pdfWriter.write(resultPdfFile)

minutesFile.close()

resultPdfFile.close()

#####################################################加密PDF###########################################################

import PyPDF2

pdfFile=file(r'C:\Users\Administrator\Desktop\deMontjoye.SM.pdf','rb')

pdfReader=PyPDF2.PdfFileReader(pdfFile)

pdfWriter=PyPDF2.PdfFileWriter()

for pageNum in range(pdfReader.numPages):

pdfWriter.addPage(pdfReader.getPage(pageNum))

pdfWriter.encrypt('swordfish')

resultPdf=file(r'C:\Users\Administrator\Desktop\t.pdf','wb')

pdfWriter.write(resultPdf)

resultPdf.close()

python自动化之PDF的更多相关文章

  1. Python自动化运维:技术与最佳实践 PDF高清完整版|网盘下载内附地址提取码|

    内容简介: <Python自动化运维:技术与最佳实践>一书在中国运维领域将有“划时代”的重要意义:一方面,这是国内第一本从纵.深和实践角度探讨Python在运维领域应用的著作:一方面本书的 ...

  2. Python自动化运维 技术与最佳实践PDF高清完整版免费下载|百度云盘|Python基础教程免费电子书

    点击获取提取码:7bl4 一.内容简介 <python自动化运维:技术与最佳实践>一书在中国运维领域将有"划时代"的重要意义:一方面,这是国内第一本从纵.深和实践角度探 ...

  3. Selenium2+python自动化43-判断title(title_is)

    From: https://www.cnblogs.com/yoyoketang/p/6539117.html 前言 获取页面title的方法可以直接用driver.title获取到,然后也可以把获取 ...

  4. Selenium2+python自动化54-unittest生成测试报告(HTMLTestRunner)

    前言 批量执行完用例后,生成的测试报告是文本形式的,不够直观,为了更好的展示测试报告,最好是生成HTML格式的. unittest里面是不能生成html格式报告的,需要导入一个第三方的模块:HTMLT ...

  5. Selenium2+python自动化61-Chrome您使用的是不受支持的命令行标记:--ignore-certificate-errors

    前言 您使用的是不受支持的命令行标记:--ignore-certificate-errors.稳定性和安全性会有所下降 selenium2启动Chrome浏览器是需要安装驱动包的,但是不同的Chrom ...

  6. Selenium2+python自动化59-数据驱动(ddt)

    前言 在设计用例的时候,有些用例只是参数数据的输入不一样,比如登录这个功能,操作过程但是一样的.如果用例重复去写操作过程会增加代码量,对应这种多组数据的测试用例,可以用数据驱动设计模式,一组数据对应一 ...

  7. Selenium2+python自动化55-unittest之装饰器(@classmethod)

    前言 前面讲到unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间. 于是就想是不是可以 ...

  8. Selenium2+python自动化52-unittest执行顺序

    前言 很多初学者在使用unittest框架时候,不清楚用例的执行顺序到底是怎样的.对测试类里面的类和方法分不清楚,不知道什么时候执行,什么时候不执行. 本篇通过最简单案例详细讲解unittest执行顺 ...

  9. selenium3+python自动化50-环境搭建(firefox)

    前言 有不少小伙伴在安装selenium环境后启动firefox报错,因为现在selenium升级到3.0了,跟2.0的版本还有有一点区别的. 安装环境过程中主要会遇到三个坑: 1.'geckodri ...

随机推荐

  1. expect 批量执行命令

    在跳板机上执行脚本,登录到远程机器分区格式化挂载命令 #!/bin/bashpasswd='engine'/usr/bin/expect <<-EOFset time 40spawn ss ...

  2. 写个定时任务更新svn

    最近学了点shell编程,寻思锻炼下写一个.平常你学习或者看别人讲,自己不练习肯定不行,基本上一动手准出错哈哈.等自己去实践,才会知道哪里有问题,哪里容易出错,哪里要注意什么的. 因为我们每个人有自己 ...

  3. 第13章 GPIO—位带操作

    第13章     GPIO—位带操作 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fire ...

  4. odoo之recoed.append()方法

    # 这里只是带数据到订单里面去,所以append要加append((0,0,)数据已经存在,放入到表中if dict: for line in dict: record.append((0,0,{'s ...

  5. 文件I/O(2)

    文件I/O(2) 文件共享 内核使用三种数据结构表示打开的文件,他们之间的关系决定了在文件共享方面一个进程对还有一个进程可能产生的影响.如图1所看到的. 1)  每一个进程在进程表中都有一个记录项.记 ...

  6. [Oracle]坏块处理:确认坏块的对象

    如果已经知道 FILE#,BLOCK#,则 可以通过如下查询来看: SQL> SELECT SEGMENT_TYPE,OWNER||'.'||SEGMENT_NAME FROM DBA_EXTE ...

  7. Windows下的Anaconda+OpenCV的环境配置

    Windows下的Anaconda+OpenCV的环境配置

  8. Linux rhel7 无线网络配置

    前言: 手提新装rhel7, ifconfig 发现只有lo 怎么办? 1. 检查网卡驱动装了没有: nmcli -a|grep wlp\ 如果没安装: a. lspci|grep Wireless ...

  9. eclipse + maven + com.sun.jersey 创建 restful api

    maven 创建 jersey 项目 如果没找到 jersey archetype, 下载 maven 的 archetype xml, 然后导入 archetypes 运行 右击 main.java ...

  10. fis入门-单文件编译之文件优化(optimize)

    FIS(Front-end Integrated Solution ),是百度的前端集成解决方案.最近几天在研究前端构建的东西,就顺便了解了下,果断各种高大上,可以到FIS官网围观感受一下.如果对fi ...