裁剪后的python pdf空白

裁剪后的python pdf空白,python,pdf,pypdf,Python,Pdf,Pypdf,我已经读到PyPDF在使用python裁剪PDF时可能会出现问题。有没有人能帮我理解为什么我的脚本会被截取,而文件却保持空白 def cropPDF(filenamePDF): top = 57 ############################### right = 26 # Margin's to be trimmed # bottom = 75 # in pixels # left =

我已经读到PyPDF在使用python裁剪PDF时可能会出现问题。有没有人能帮我理解为什么我的脚本会被截取,而文件却保持空白

def cropPDF(filenamePDF):
    top = 57      ###############################
    right = 26    #   Margin's to be trimmed    #
    bottom = 75   #          in pixels          #
    left = 26     ###############################
    pdfIn = PdfFileReader(open(filenamePDF,'rb'))
    pdfOut = PdfFileWriter()
    for page in pdfIn.pages:
        page.mediaBox.upperRight   =  (page.mediaBox.getUpperRight_x() - right, page.mediaBox.getUpperRight_y() -top)
        page.mediaBox.lowerLeft    =  (page.mediaBox.getLowerLeft_x() - left, page.mediaBox.getLowerLeft_y() -bottom)
        pdfOut.addPage(page)
        ous = open(filenamePDF, 'wb')
        pdfOut.write(ous)
        ous.close()

问题可能是您正在裁剪文档的一小块区域,该区域可能是空白的,也可能不是空白的

有一个问题,应该有你的答案

#!/usr/bin/python
#

from pyPdf import PdfFileWriter, PdfFileReader

with open("in.pdf", "rb") as in_f:
    input1 = PdfFileReader(in_f)
    output = PdfFileWriter()

    numPages = input1.getNumPages()
    print "document has %s pages." % numPages

    for i in range(numPages):
        page = input1.getPage(i)
        print page.mediaBox.getUpperRight_x(), page.mediaBox.getUpperRight_y()
        page.trimBox.lowerLeft = (25, 25)
        page.trimBox.upperRight = (225, 225)
        page.cropBox.lowerLeft = (50, 50)
        page.cropBox.upperRight = (200, 200)
        output.addPage(page)

    with open("out.pdf", "wb") as out_f:
        output.write(out_f)

作为旁白:“以像素为单位修剪边距”-您将这些值用作点,而不是像素。