Python应用【PDF处理-pypdf2】
2024-10-10 15:02:21
概述
Python处理PDF文件需要安装相应的库:【PyPDF2】库
使用场景
工作中可能会涉及处理pdf文件,PyPDF2就是这样一个库, 使用它可以轻松的处理 pdf 文件,它提供了读、写、分割、合并、文件转换等多种操作。
安装库
使用说明
PyPDF2 官方文档(documentation)
学习网站
博客
# 导入相关类
from PyPDF2 import PdfFileReader, PdfFileWriter
# 定义输入文件和输出文件(路径)
inputFile = 'read.pdf'
outputFile = 'write.pdf'
# 获取一个 PdfFileReader 对象
pdfReader = PdfFileReader(open(inputFile, 'rb'))
# 获取 PDF 的页数
pageCount = pdfReader.getNumPages()
print(pageCount)
# 返回一个 PageObject
page = pdfReader.getPage(i)
# 获取一个 PdfFileWriter 对象
pdfWriter = PdfFileWriter()
# 将一个 PageObject 加入到 PdfFileWriter 中
pdfWriter.addPage(page)
# 输出到文件中
pdfWriter.write(open(outputFile, 'wb'))
GitHub
提供了使用案例,当我们使用的时候再具体查看。
最新文章
- html4,xhtml,html5发展历史
- Android View各种尺寸位置相关的方法探究
- Android资料之-EditText中的inputType
- Winform添加Label
- bootstrap第一天,响应式布局,栅格系统运用
- OD使用心得
- PS4 的下载速度问题
- UITapGestureRecognizer 的用法
- QQ音乐API
- 理解display:inline、block、inline-block
- setTimeout setInterval 区别 javascript线程解释
- SQL SERVER SQLOS的任务调度
- java测试之文件操作
- java窗口按钮设置五个方向
- Python[小甲鱼-002用Python设计第一个游戏]
- Java基本语法-----java数据类型的转换
- Ambari安装HDP问题:User root is not allowed to impersonate anonymous.User: hcat is not allowed to impersonate ambari-qa
- Oracle 关键字、高级函数的使用
- 注册asp.net 4.0版本到IIS服务器中
- Java Web之EL
热门文章
- JavaScript实现的抛物线运动效果
- 属性(property)的特性(attribute)
- zabbix 3.2.4 使用详解
- openmp查看最大线程数量
- Nginx限制下载速度
- Android NDK开发调试
- VMware搭建虚拟机服务器
- [转] react-router4 + webpack Code Splitting
- some advice in work
- System.Data.Entity.Internal.AppConfig";的类型初始值设定项引发异常