【问题标题】:Crop a pdf page to content将 pdf 页面裁剪为内容
【发布时间】:2018-11-27 18:14:27
【问题描述】:

使用 Python,是否可以将 pdf 页面裁剪为如下图所示在 Inkscape 中完成任务的内容?应该会自动找到内容的边界区域。

使用 PyPDF2 可以裁剪页面,但需要手动查找坐标,这对于大量文件来说很繁琐。在 Inkscape 中,会自动找到坐标。

我使用的代码如下所示,示例输入文件是available here

# Python 3.7.0
import PyPDF2 # version 1.26.0

with open('document-1.pdf','rb') as fin:
    pdf = PyPDF2.PdfFileReader(fin)
    page = pdf.getPage(0)

    # Coordinates found by inspection.
    # Can these coordinates be found automatically?
    page.cropBox.lowerLeft=(88,322)
    page.cropBox.upperRight = (508,602)

    output = PyPDF2.PdfFileWriter()
    output.addPage(page)

    with open('cropped-1.pdf','wb') as fo:
        output.write(fo)

【问题讨论】:

    标签: python pdf


    【解决方案1】:

    我可以使用 pip 可安装的 CLI https://pypi.org/project/pdfCropMargins/ 来做到这一点

    由于我最初回答,已添加 Python 接口:https://github.com/abarker/pdfCropMargins#python-interface (h/t @Paul)

    我从命令行调用它的原始答案如下。


    不幸的是,我认为没有直接从脚本调用它的好方法,所以现在我使用os.system

    $ python -m pip install pdfCropMargins --user
    $ pdf-crop-margins document.pdf -o output.pdf -p 0
    
    import os
    os.system('pdf-crop-margins document.pdf -o output.pdf -p 0')
    

    【讨论】:

    猜你喜欢
    • 2016-07-26
    • 2011-03-23
    • 1970-01-01
    • 2010-10-02
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多