从 Python 中的预定义行裁剪 PDF 文件

问题描述 投票:0回答:0

我正在尝试从定义的行中剪切 PDF 的特定区域。考虑到整个 PDF 文件,我希望从这一行剪下 PDF。我目前正在使用的代码剪辑了所需的区域,但它也考虑了指定线上方的区域,我希望它位于指定线下方。

我的代码:

# Open PDF file
with open('file.pdf', 'rb') as pdf_file:
    # Create object
    pdf_reader = PyPDF2.PdfReader(pdf_file)

    # Create object
    pdf_writer = PyPDF2.PdfWriter()

    # Line
    x1, y1 = (int(66 * 2.83465), int(204 * 2.83465))  # ponto esquerdo da linha
    x2, y2 = (int(143.7 * 2.83465), int(204 * 2.83465))  # ponto direito da linha
    y3 = int(y1 -  100 * 2.83465)  # altura do corte

    # All pages
    for page_num in range(len(pdf_reader.pages)):
        # Obtenha a página atual
        page = pdf_reader.pages[page_num]

        # Create a new page
        new_page = PyPDF2.PageObject.create_blank_page(
            None, page.mediabox.width, y3 - y1)

        # Define area
        new_page.mediabox.upper_left = (x1, y1 + y3)
        new_page.mediabox.upper_right = (x2, y1)

        # Add new page
        transformation = PyPDF2.Transformation().translate(0, -y1)
        new_page.add_transformation(transformation)
        new_page.merge_page(page)
        pdf_writer.add_page(new_page)

    # Save
    with open('cropped_file.pdf', 'wb') as new_pdf_file:
        pdf_writer.write(new_pdf_file)
python pdf crop
© www.soinside.com 2019 - 2024. All rights reserved.