首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyPdf:将每一页分割成两部分,垫上空白

PyPdf:将每一页分割成两部分,垫上空白
EN

Stack Overflow用户
提问于 2015-07-22 08:31:43
回答 1查看 2K关注 0票数 2

我有一个PDF文件(A4,肖像布局),每一页我想分裂在一半的高度。输出文档也应该是A4和纵向布局,但是每个页面的下半部分需要是空白的。

我看到了https://stackoverflow.com/a/15743413/822789,但不知道如何在mediaBox中添加空格。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-26 17:55:00

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

代码语言:javascript
复制
#!/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)
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31557909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档