###################################处理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. char a='1'和char a=1区别

    char a='1'表示:把字符为1,ASSIC码为49的值赋值给a: char a= 1表示:把ASSIC码为1的值赋值给a

  2. (转)postfix疯狂外发垃圾邮件之分析与解决

    从进程中看到,好像是postfix有问题.我这postfix主要是用来给程序发达邮件用的,如报警,程序外发邮件等.平时postfix进程不会像现在这样异常,这在postf主进程CPU占用高,其它的相关 ...

  3. day46

    CSS选择器 盒模型 标签a.img.list 伪类选择器 CSS选择器 一.基础选择器 1.通配选择器 - { border: solid; } 匹配文档中所有标签:通常指html.body及bod ...

  4. linux中分区、格式化文件系统、挂载

    以前学linux的时候,毕竟自己没搞运维,就只注重了很多命令的运用,没太在意文件系统这块.买了本linux的书,这部分看了点东西,记个笔记哈哈. 有个场景,比如说我们现在的服务器上存储不够用了,那么当 ...

  5. 2017-2018-2 20155203《网络对抗技术》Exp6 信息搜集与漏洞扫描

    1.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 2.实践内容 (1)各种搜索技巧的应用 通过搜索引擎进行信息搜集敏感信息 在百度搜索栏中输入filetype:关键字 site:edu.c ...

  6. arm学习之汇编跳转指令总结

    目前所知道的跳转指令有 b,bl,bep,bne.他们共同点是都是以b开头,首先从字面上分析:b:是Branch,表示分支.bl:是Branch Link表示带连接的分支.bep:Branch ,Eq ...

  7. 变量内存空间的释放---c语言

    堆栈内存释放: 栈的内存是由编译器自动分配.释放,出了作用域就释放. 堆的内存由程序员分配.释放,他的作用域是整个程序,如果程序没有释放,程序结束时会自动释放.

  8. 4.Xilinx RapidIO核详解

    转自https://www.cnblogs.com/liujinggang/p/10072115.html 一.RapidIO核概述 RapidIO核的设计标准来源于RapidIO Interconn ...

  9. python中的and和or用法

    在python中and和or返回的值并不是True和false这么简单.虽然他们看上去和c++中的&&和||有些相似.在了解and和or之前,我们先要了解python中的True和Fa ...

  10. 重新解读DDD领域驱动设计(一)

    回顾 十年前,还未踏入某校时,便听闻某学长一毕业就入职北京某公司,月薪过万.对于一个名不见经传的小学院,一毕业能拿到这个薪水还是非常厉害的.听闻他学生期间参与开发了一款股票软件,股票那时正迎来一波疯涨 ...