我正在使用 python 来裁剪 pdf 页面。一切正常,但是如何更改页面大小(宽度)?
这是我的作物代码:
input = PdfFileReader(file('my.pdf', 'rb'))
p = input.getPage(1)
(w, h) = p.mediaBox.upperRight
p.mediaBox.upperRight = (w/4, h)
output.addPage(p)
当我裁剪页面时,我也需要调整它们的大小,我该怎么做?
这个答案真的姗姗来迟,也许旧版本的 PyPDF2 没有这个功能,但它实际上在 1.26.0 版本中非常简单
import PyPDF2
pdf = "YOUR PDF FILE PATH.pdf"
pdf = PyPDF2.PdfFileReader(pdf)
page0 = pdf.getPage(0)
page0.scaleBy(0.5) # float representing scale factor - this happens in-place
writer = PyPDF2.PdfFileWriter() # create a writer to save the updated results
writer.addPage(page0)
with open("YOUR OUTPUT PDF FILE PATH.pdf", "wb+") as f:
writer.write(f)
您还可以使用以下函数直接在合并函数调用中应用缩放、旋转或平移:
或者在页面对象上使用 addTransformation()。
裁剪后要缩放图像吗?你可以用它p.scale(factor_x, factor_y)
来做到这一点。