python 如何使用pyPdf沿着中间分割/裁剪pdf

x759pob2  于 2022-12-17  发布在  Python
关注(0)|答案(3)|浏览(214)

我有一个看起来像this的pdf,我想裁剪掉页面中间的所有文本。

def splitHorizontal():
from pyPdf import PdfFileWriter, PdfFileReader
input1 = PdfFileReader(file("in.pdf", "rb"))
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)

outputStream = file("out.pdf", "wb")
output.write(outputStream)
outputStream.close()

然而,这些作物尺寸是调整到那个特定的例子。谁能告诉我如何找到正确的作物尺寸。

lb3vh1jj

lb3vh1jj1#

我最初是从这里得到脚本的--〉Cropping pages of a .pdf file
我仔细阅读了作者的话,终于意识到他说的是:
生成的文档有一个200 x200磅的裁切框,从媒体框内的25,25磅处开始。裁切框在裁切框内的25磅处。
意义

page.cropBox.upperRight = (200, 200)

必须控制最终利润,因此我将报表调整为

page.cropBox.upperLeft = (290, 792)

将裁剪镜像到另一侧并确保裁剪保持完整的垂直值

3zwjbxry

3zwjbxry2#

将每页切成两半(例如,如果源文件是以小册子形式创建的),然后将其重新组合以进行进一步处理(例如,文本提取)
导入所需的库

from PyPDF2 import PdfFileWriter,PdfFileReader,PdfFileMerger

拆分左侧部分

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

    numPages = input1.getNumPages()

    for i in range(numPages):
        page = input1.getPage(i)
        page.cropBox.lowerLeft = (60, 50)
        page.cropBox.upperRight = (305, 700)
        output.addPage(page)

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

拆分右侧部分:

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

    numPages = input1.getNumPages()

    for i in range(numPages):
        page = input1.getPage(i)
        page.cropBox.lowerLeft = (300, 50)
        page.cropBox.upperRight = (540, 700)
        output.addPage(page)

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

左右组合(两栏两页)

input1 = PdfFileReader(open("left.pdf","rb"))
input2 = PdfFileReader(open("right.pdf","rb"))
output = PdfFileWriter()
numPages = input1.getNumPages()

for i in range(numPages):
    l = input1.getPage(i)
    output.addPage(l)
    r = input2.getPage(i)
    output.addPage(r)

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

cxfofazt3#

我面临着同样的挑战,我写了这个(需要PyPDF2):
https://gist.github.com/kintaro1981/eb6cfc6f40a7fb39744f5ae630d58fd5

# -*- coding: utf-8 -*-
"""
This script splits each pdf page in half vertically and merge them in a single PDF in order.
It asks how many pages do you want to skip to avoid splitting a book cover or a specific number of initial pages.
Usage:
python cutpdfpages.py <filename.pdf> <newfilename.pdf>
"""

import sys
import copy
from PyPDF2 import PdfWriter, PdfReader

file = str(sys.argv[1])
newfile = str(sys.argv[2])

with open(file, "rb") as pdf1:
    pdf = PdfReader(pdf1)
    output = PdfWriter()
    numpages = pdf.getNumPages()
    page2skip =  int(input('Insert how many pages do you want to skip: '))

    for i in range(page2skip, numpages):
        page = pdf.pages[i]
        pagebis = copy.copy(page)
        page.mediabox.upper_left = (page.mediabox.right / 2, page.mediabox.top,)
        output.add_page(page)
        pagebis.mediabox.upper_right = (pagebis.mediabox.right / 2, pagebis.mediabox.top,)
        output.add_page(pagebis)

    with open(newfile, "wb") as newpdf:
        output.write(newpdf)

相关问题