浅谈Python处理PDF的方法

yipeiwu_com6年前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命令方法分享等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!

相关文章

解决pandas使用read_csv()读取文件遇到的问题

如下: 数据文件: 上海机场 (sh600009) 24.11 3.58...

python轻松实现代码编码格式转换

  最近刚换工作不久,没太多的时间去整理工作中的东西,大部分时间都在用来熟悉新公司的业务,熟悉他们的代码框架了,最主要的是还有很多新东西要学,我之前主要是做php后台开发的,来这边之后还...

解决Python3中的中文字符编码的问题

解决Python3中的中文字符编码的问题

python3中str默认为Unicode的编码格式 Unicode是一32位编码格式,不适合用来传输和存储,所以必须转换成utf-8,gbk等等 所以在Python3中必须将str类型...

python里运用私有属性和方法总结

python里运用私有属性和方法总结

如何在PYTHON里运用私有属性和方法 class File: def __init__(self, name): self.name = name sel...

Python进阶篇之字典操作总结

一、与字典值有关的计算 问题 想对字典的值进行相关计算,例如找出字典里对应值最大(最小)的项。 解决方案一: 假设要从字典 {'a':3, 'b':2, 'c':6} 中找出值最小...