浅谈Python处理PDF的方法

yipeiwu_com5年前Python基础

处理pdf文档

第一、

从文本中提取文本

第二、

创建PDF

两种方法

#使用PdfFileWriter
import PyPDF2
 
pdfFiles = []
for filename in os.listdir('.'):
if filename.endswith('.pdf'):
pdfFiles.append(filename)
print(pdfFiles)
pdfWriter = PyPDF2.PdfFileWriter()
 
pdfFileObj = open(pdfFiles[0],'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj) # 得到PdfFileReader对象
first,end =map(int,input('从多少页到多少页(用空格隔开):').split())
for pageNum in range(first-1,end):
pageObj = pdfReader.getPage(pageNum)
pdfWriter.addPage(pageObj)
pdfOutput = open ('split_pdf.pdf','wb')
pdfWriter.write(pdfOutput)
pdfOutput.close()

#使用PdfFileMerger()
import PyPDF2
merger = PyPDF2.PdfFileMerger()
a = [str(i)+'webbook.pdf'for i in range(0,124)]
for i in a:
print(i)
merger.append(open(i,'rb'))
print("合并完成第"+str(i))
with open('combintion.pdf','wb') as f:
merger.write(f)

总结

以上就是本文关于浅谈Python处理PDF的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:python先序遍历二叉树问题python实现人脸识别代码python执行使用shell命令方法分享等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!

相关文章

一文带你了解Python中的字符串是什么

一文带你了解Python中的字符串是什么

在《 详解Python拼接字符串的七种方式 》这篇文章里,我提到过,字符串是程序员离不开的事情。后来,我看到了一个英文版本的说法: There are few guarantees in...

Python requests发送post请求的一些疑点

Python requests发送post请求的一些疑点

前言 在Python爬虫中,使用requests发送请求,访问指定网站,是常见的做法。一般是发送GET请求或者POST请求,对于GET请求没有什么好说的,而发送POST请求,有很多朋友不...

python中使用 xlwt 操作excel的常见方法与问题

前言 Python可以操作Excel的模块不止一种,我习惯使用的写入模块是xlwt(一般都是读写模块分开的) python中使用xlwt操作excel非常方,和Java使用调框架apac...

Numpy截取指定范围内的数据方法

如下所示: lst = [[1,2,3,4,5,6], [7,8,9,10,11,12], [71,81,91,101,111,121]] arr = np.asarray(lst)...

python里将list中元素依次向前移动一位

问题 定义一个int型的一维数组,包含10个元素,分别赋值为1~10, 然后将数组中的元素都向前移一个位置, 即,a[0]=a[1],a[1]=a[2],…最后一个元素的值是原来第一个元...