python操作PDF------旋转及排序pdf

时间:2024-02-20 20:12:20
# 安装 pip install pypdf2
from PyPDF2 import PdfFileReader, PdfFileWriter


# 用pypdf2旋转PDF某一页
pdf_reader = PdfFileReader(\'基于python的网页爬虫.pdf\')
pdf_writer = PdfFileWriter()
# 顺时针旋转90度  90的倍数
page = pdf_reader.getPage(0).rotateClockwise(90)
pdf_writer.addPage(page)
# 逆时针旋转90度  90的倍数
page = pdf_reader.getPage(1).rotateCounterClockwise(90)
pdf_writer.addPage(page)
with open(\'rotated.pdf\', \'wb\') as f:
    pdf_writer.write(f)


# 排序pdf页面  此处例子为倒序排序
pdf_reader = PdfFileReader(\'基于python的网页爬虫.pdf\')
pdf_writer = PdfFileWriter()
for page in range(pdf_reader.getNumPages()-1, -1, -1):
    pdf_writer.addPage(pdf_reader.getPage(page))
with open(\'reordered.pdf\', \'wb\') as f:
    pdf_writer.write(f)