【问题标题】:Python script to remove blank pages using pyPDF使用 pyPDF 删除空白页的 Python 脚本
【发布时间】:2011-09-12 16:58:08
【问题描述】:

我正在尝试使用 pyPDF 编写几个 python 脚本将 PDF 页面拆分为六个单独的页面,正确排序它们(通常打印正面和背面,因此每个其他页面都需要以不同的方式对其子页面进行排序),并删除结果输出文档末尾的空白页。

我编写了以下脚本来剪切 PDF 页面并重新排序。将每页分成两列,每列分成三页。我对python不是很有经验,所以请原谅我做的不对。

#!/usr/bin/env python
import copy, sys
from pyPdf import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()

for i in range(0,input.getNumPages(),2):
    p = input.getPage(i)
    q = copy.copy(p)
    r = copy.copy(p)
    s = copy.copy(p)
    t = copy.copy(p)
    u = copy.copy(p)
    (x, y) = p.mediaBox.lowerLeft
    (w, h) = p.mediaBox.upperRight

    p.mediaBox.lowerLeft = (x, 2 * h / 3)
    p.mediaBox.upperRight = (w / 2, h)

    q.mediaBox.lowerLeft = (w / 2, 2 * h / 3)
    q.mediaBox.upperRight = (w, h)

    r.mediaBox.lowerLeft = (x, h / 3)
    r.mediaBox.upperRight = (w / 2, 2 * h / 3)

    s.mediaBox.lowerLeft = (w / 2, h / 3)
    s.mediaBox.upperRight = (w, 2 * h / 3)

    t.mediaBox.lowerLeft = (x, y)
    t.mediaBox.upperRight = (w / 2, h / 3)

    u.mediaBox.lowerLeft = (w / 2, y)
    u.mediaBox.upperRight = (w, h / 3)

    a = input.getPage(i+1)
    b = copy.copy(a)
    c = copy.copy(a)
    d = copy.copy(a)
    e = copy.copy(a)
    f = copy.copy(a)
    (x, y) = a.mediaBox.lowerLeft
    (w, h) = a.mediaBox.upperRight

    a.mediaBox.lowerLeft = (x, 2 * h / 3)
    a.mediaBox.upperRight = (w / 2, h)

    b.mediaBox.lowerLeft = (w / 2, 2 * h / 3)
    b.mediaBox.upperRight = (w, h)

    c.mediaBox.lowerLeft = (x, h / 3)
    c.mediaBox.upperRight = (w / 2, 2 * h / 3)

    d.mediaBox.lowerLeft = (w / 2, h / 3)
    d.mediaBox.upperRight = (w, 2 * h / 3)

    e.mediaBox.lowerLeft = (x, y)
    e.mediaBox.upperRight = (w / 2, h / 3)

    f.mediaBox.lowerLeft = (w / 2, y)
    f.mediaBox.upperRight = (w, h / 3)

    output.addPage(p)
    output.addPage(b)
    output.addPage(q)
    output.addPage(a)
    output.addPage(r)
    output.addPage(d)
    output.addPage(s)
    output.addPage(c)
    output.addPage(t)
    output.addPage(f)
    output.addPage(u)
    output.addPage(e)

output.write(sys.stdout)

然后我使用以下脚本删除空白页。

#!/usr/bin/env python
import copy, sys
from pyPdf import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()

for i in range(0,input.getNumPages()):
    p = input.getPage(i)

    text = p.extractText()

    if (len(text) > 10):
        output.addPage(p)

output.write(sys.stdout)

问题似乎是,当页面明显被裁剪时,文本绘制命令仍然存在。这些页面都没有被扫描,所以如果它们是空白的,它们就真的是空白的。有没有人对我可以做不同的事情或可能采取完全不同的方法来删除空白页有任何想法?非常感谢任何帮助。

【问题讨论】:

  • 更新:如果我在 Acrobat 中打开第一个脚本的结果,然后使用“另存为...”,它会从头开始重建文件。然后,当我运行第二个脚本时,它会按照我想要的方式工作。我需要一种方法来编写 acrobat 在“另存为...”中执行的过程的脚本,以便在不需要 Acrobat 本身的脚本中使用。
  • 这不是一个真正的答案,所以我将它作为评论发布。完成后,需要将整个内容放入 iOS 应用程序中。我只用绘制原始 PDF 的适当区域,并设置了最后的空白页数。这并不理想,但它确实有效。

标签: python pdf crop pypdf


【解决方案1】:

PdfFileReader 有一个方法,getPage(self, page number),它返回一个对象,PageObject,它又有一个方法getContents,如果页面是空白的,它将返回None。因此,使用您的 pdf 对象 getNumPages(),使用 if getPage(i).getContents(): 进行迭代,将点击收集到要输出的页码列表中。

【讨论】:

  • 谢谢!我显然一直在使用几年前没有 getContents() 方法的 pyPdf 副本。我没有测试过这个解决方案,因为我采用了原始帖子评论中描述的路线(不是解决方案,完全不同的路线)。如果我需要回到这个,我将使用 getContents() 作为起点。
猜你喜欢
  • 2017-04-10
  • 2022-10-31
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 2016-03-15
  • 2021-05-11
  • 2021-02-23
  • 1970-01-01
相关资源
最近更新 更多