Using Python, is it possible to crop a pdf page to the content as shown in the image below where the task is achieved in Inkscape? The bounding area for the content should be found automatically.
Using PyPDF2 I can crop the page, but it requires the coordinates to be manually found, which is tedious for a large number of files. In Inkscape, the coordinates are automatically found.
The code I'm using is shown below and an example input file is 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)