如何修复pypdf裁剪页面

时间:2019-06-29 08:50:07

标签: python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from PyPDF2 import PdfFileWriter,PdfFileReader,PdfFileMerger
output = PdfFileWriter()
pdf_file = PdfFileReader(open("1.pdf","rb"))
i=0
page = pdf_file.getPage(i)
page1 = pdf_file.getPage(i)
page_2 = pdf_file.getPage(i)
print (page.mediaBox.getUpperRight_x(), page.mediaBox.getUpperRight_y())
pdf_half_y=page.mediaBox.getUpperRight_y()/2 # 792/2
pdf_y=page.mediaBox.getUpperRight_y()        # 792
pdf_x=page.mediaBox.getUpperRight_x()        # 612

# FBA
page1.mediaBox.lowerRight = (pdf_x/2, pdf_half_y+20)  # 右下
page1.mediaBox.lowerLeft = (0, pdf_half_y+20)     # 左下
page1.mediaBox.upperRight = (pdf_x/2, pdf_y+20)# 右上
page1.mediaBox.upperLeft = (0, pdf_y)   # 左上
output.addPage(page1)
with open("out1.pdf", "wb") as out_f1:
    output.write(out_f1)

这部分我有两页,应该是一页,有人可以告诉我为什么吗?

# Carrier
page_2.mediaBox.lowerRight = (pdf_x, 0)  # 右下
page_2.mediaBox.lowerLeft = (0, 0)     # 左下
page_2.mediaBox.upperRight = (pdf_x, pdf_half_y)# 右上
page_2.mediaBox.upperLeft = (0, pdf_half_y)   # 左上
page_2.rotateClockwise(90)
output.addPage(page_2)
with open("out2.pdf", "wb") as out_f2:
    output.write(out_f2)

0 个答案:

没有答案