gpt4 book ai didi

pdf - PyPdf:将每页一分为二,用空格填充

转载 作者:行者123 更新时间:2023-12-01 22:27:35 27 4
gpt4 key购买 nike

我有一个 PDF 文件(A4,纵向布局),我想将其中的每一页分成一半高度。输出文件也应该是A4和纵向布局,但每页的下半部分需要是空白的。

我看到了https://stackoverflow.com/a/15743413/822789但不明白如何使用 mediaBox 添加空格。

最佳答案

我不太了解 PyPDF2,但我是 pdfrw 的作者如果我理解你的问题,pdfrw 当然可以很容易地做你想做的事。我需要更好地记录它,但我有一个预先存在的 unspread.py左右拆分页面的示例,将小报页面切成原始页面。这是该示例的修改版本。此版本将顶部和底部拆分页面,并更改输出页面的大小,使其与输入页面相匹配:

#!/usr/bin/env python

'''
usage: splitv.py my.pdf

Creates splitv.my.pdf

This is similar to unspread.py, in that it creates
a new file that has twice the pages of the old file.

It is different in two ways:

1) It splits pages top and bottom rather than left and right
2) The destination pages are the same size as the source pages,
and the output is placed at the top.
'''

import sys
import os

from pdfrw import PdfReader, PdfWriter, PageMerge


def splitpage(src):
''' Split a page into two (top and bottom)
'''
# Yield a result for each half of the page
for y_pos in (0, 0.5):

# Create a blank, unsized destination page.
page = PageMerge()

# add a portion of the source page to it as
# a Form XObject.
page.add(src, viewrect=(0, y_pos, 1, 0.5))

# By default, the object we created will be
# at coordinates (0, 0), which is the lower
# left corner. To move it up on the page
# to the top, we simply use its height
# (which is half the source page height) as
# its y value.
page[0].y = page[0].h

# When we render the page, the media box will
# encompass (0, 0) and all the objects we have
# placed on the page, which means the output
# page will be the same size as the input page.
yield page.render()


inpfn, = sys.argv[1:]
outfn = 'splitv.' + os.path.basename(inpfn)
writer = PdfWriter()
for page in PdfReader(inpfn).pages:
writer.addpages(splitpage(page))
writer.write(outfn)

关于pdf - PyPdf:将每页一分为二,用空格填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31557909/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com