python修改pdf内容_python3.6调整字体Python处理pdf文件库 - PyPDF2详解

PyPDF2是一个用于处理PDF文件的Python库,支持读取、写入、合并、分割PDF文档。本文介绍了如何使用PyPDF2进行简单读写、合并分割PDF,并给出了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工作中可能会涉及处理pdf文件,PyPDF2就是这样一个库, 使用它可以轻松的处理pdf文件,它提供了读、写、分割、合并、文件转换等多种操作。官方地址:http://mstamy2.github.io/PyPDF2/

安装

1. RPM式系统(Redhat、CentOS)pip install pypdf2

2. DEB式系统(Debian、Ubuntu)以下任一pip install pypdf2

apt install python-pypdf2

3. Windowspip install pypdf2

使用

PyPDF2 包含了 PdfFileReader PdfFileMerger PageObject PdfFileWriter 四个常用的主要 Class。

简单读写from PyPDF2 import PdfFileReader, PdfFileWriter

readFile = 'read.pdf'

writeFile = 'write.pdf'

# 获取一个 PdfFileReader 对象

pdfReader = PdfFileReader(open(readFile, 'rb'))

# 获取 PDF 的页数

pageCount = pdfReader.getNumPages()

print(pageCount)

# 返回一个 PageObject

page = pdfReader.getPage(i)

# 获取一个 PdfFileWriter 对象

pdfWriter = PdfFileWriter()

# 将一个 PageObject 加入到 PdfFileWriter 中

pdfWriter.addPage(page)

# 输出到文件中

pdfWriter.write(open(writeFile, 'wb'))

合并分割 PDFfrom PyPDF2 import PdfFileReader, PdfFileWriter

def split_pdf(infn, outfn):

pdf_output = PdfFileWriter()

pdf_input = PdfFileReader(open(infn, 'rb'))

# 获取 pdf 共用多少页

page_count = pdf_input.getNumPages()

print(page_count)

# 将 pdf 第五页之后的页面,输出到一个新的文件

for i in range(5, page_count):

pdf_output.addPage(pdf_input.getPage(i))

pdf_output.write(open(outfn, 'wb'))

def merge_pdf(infnList, outfn):

pdf_output = PdfFileWriter()

for infn in infnList:

pdf_input = PdfFileReader(open(infn, 'rb'))

# 获取 pdf 共用多少页

page_count = pdf_input.getNumPages()

print(page_count)

for i in range(page_count):

pdf_output.addPage(pdf_input.getPage(i))

pdf_output.write(open(outfn, 'wb'))

if __name__ == '__main__':

infn = 'infn.pdf'

outfn = 'outfn.pdf'

split_pdf(infn, outfn)

其他命令

如果是要修改一个已有的 pdf 文件,可以将 reader 的页面添加到 writer 中:

pdfWriter.appendPagesFromReader(reader)

添加书签:

《python3.6调整字体Python处理pdf文件库 - PyPDF2详解》总结了关于python培训教程,对于我们来电脑技术网确实能学到不少知识。

pdfWriter.addBookmark(title, pagenum, parent=parent)

更多:python3.6调整字体Python处理pdf文件库 - PyPDF2详解

https://www.002pc.comhttps://www.002pc.com/python/2231.html

你可能感兴趣的Python,PyPDF2,pdf,详解,文件,处理

js判断上午下午JavaScript代码生成PDF文件的方法

如何使用js生成pdf文件呢?相信很多朋友都被这一问题迷惑着,下面由第二电脑网小编通过本文给大家简单介绍下。

mysql 查出数据一对多python使用unittest测试接口步奏详解

这次给大家带来python使用unittest测试接口步奏详解,python使用unittest测试接口的注意事项有哪些,下面就是实战案例,一起来看一下。

linux shell 输出日志文件Nginx 配置文件详解

Nginx配置文件详解usernginx;#用户worker_processes8;#工作进程,根据硬件调整,大于等于cpu核数error_loglogs/nginx_error.logcrit;#错误日志

xp 靶场出资建设XP系统硬件配置文件详解

电脑技术002pc网认为此文章对《xp 靶场出资建设XP系统硬件配置文件详解》说的很在理。  什么是硬件配置文件

所谓硬件配置文件,是指在启动计算机时告诉Windows应

电脑网络网站批处理文件的意义

.Cjy974 { display:none; }

后缀是bat的文件就是批处理文件,是一种文本文件。简单的说,它的作用就是自动的连续执行多条命令,批处理文件的内容就是一条一

centos 7 安装php7php使用Image Magick将PDF文件转换为JPG文件的方法

《centos 7 安装php7php使用Image Magick将PDF文件转换为JPG文件的方法》总结了关于电脑知识教程,对于我们来www.002pc.com确实能学到不少知识。

修改linux时区linux命令-文件处理命令

1,创建带空格的文件(不建议这样命名)touch "name1_name2"注意:在对这个文件进行删除、复制、剪切等时,都必须带上引号语法: touch [文件名]2,显示文件内容(

linux回车符Nginx配置文件nginx.conf中文详解

电脑技术002pc网认为此文章对《linux回车符Nginx配置文件nginx.conf中文详解》说的很

asp 域名C++中头文件和源文件详细介绍

C++中的头文件和源文件详解

一、C++编译模式

通常,在一个C++程序中,只包含两类文件――.cpp文件和.h文件。其中,.cpp文件被称作C++源文件,里面放的都是C++的源代码;而.h文件则被

php 二维数组按 值排序php 下载pdf

$file = fopen($filePath, "r"); // 打开文件 // 输入文件标签

if ($file) {

Header(&

0踩

0 赞

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值