PDF处理

合并

文件

from PyPDF2 import PdfFileMerger

merger = PdfFileMerger()
file_path = "wallex_mt103.pdf"
merger.append(file_path)
merger.append(file_path)
output_pdf_path = "merge.pdf"
merger.write(output_pdf_path)
merger.close()

内存

import io

from PyPDF2 import PdfFileMerger

merger = PdfFileMerger()
file_path = "wallex_mt103.pdf"
merger.append(file_path)
merger.append(file_path)
pdf_obj = io.BytesIO()
merger.write(pdf_obj)  # 写入内存
merger.close()
pdf_obj.seek(0)

# # 保存至文件
# output_pdf_path = "merge.pdf"
# with open(output_pdf_path, "wb") as f:
#     f.write(pdf_obj.read())

缩放

from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter

ok_pdf = "1.pdf"
err_pdf = "2.pdf"
merger_obj = PdfFileMerger()
merger_obj.append(ok_pdf)
render = PdfFileReader(err_pdf)
writer = PdfFileWriter()
pages = render.getNumPages()
for i in range(render.getNumPages()):
    page_obj = render.pages[i]
    page_obj.scaleTo(595, 842)  # 转为A4(595pt, 842pt)
    writer.addPage(page_obj)
new_err_pdf = "22.pdf"
with open(new_err_pdf, "wb") as fp:
    writer.write(fp)
merger_obj.append(new_err_pdf)
pdf_obj = "output.pdf"
merger_obj.write(pdf_obj)
merger_obj.close()

PyPDF2(1.27)

github
doc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值