Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中调整pdf页面大小_Python_Pdf_Pypdf - Fatal编程技术网

在Python中调整pdf页面大小

在Python中调整pdf页面大小,python,pdf,pypdf,Python,Pdf,Pypdf,我正在使用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) 当我裁剪页面时,我也需要调整页面大小,我如何才能做到这一点?裁剪后是否要缩放图像?您可以使用p.scalefact

我正在使用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)

当我裁剪页面时,我也需要调整页面大小,我如何才能做到这一点?

裁剪后是否要缩放图像?您可以使用p.scalefactor\u x,factor\u y来执行此操作。

裁剪图像后是否要缩放图像?您可以使用p.scalefactor\u x,factor\u y来执行此操作。

您也可以使用以下函数直接在合并函数调用中应用缩放、旋转或平移:

合并页面 合并旋转页面 合并旋转缩放页面 合并旋转缩放转换页面 合并缩放页面 合并缩放转换页面 合并转换页面 合并翻译页
或者在页面对象上使用addTransformation。

也可以使用以下函数在合并函数调用中直接应用缩放、旋转或平移:

合并页面 合并旋转页面 合并旋转缩放页面 合并旋转缩放转换页面 合并缩放页面 合并缩放转换页面 合并转换页面 合并翻译页
或者在页面对象上使用addTransformation。

这个答案早就应该给出了,也许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)

这个答案早就应该给出了,也许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)