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

# 安装 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)

 

posted @ 2020-09-04 19:14  不会飞的鲨鱼  阅读(1814)  评论(0编辑  收藏  举报