【发布时间】: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)
【问题讨论】: