from PyPDF2 import PdfWriter, PdfReader
reader = PdfReader("example.pdf")
writer = PdfWriter()
# add page 1 from reader to output document, unchanged:
writer.add_page(reader.pages[0])
# add page 2 from reader, but rotated clockwise 90 degrees:
writer.add_page(reader.pages[1].rotate(90))
# add page 3 from reader, but crop it to half size:
page3 = reader.pages[2]
page3.mediabox.upper_right = (
page3.mediabox.right / 2,
page3.mediabox.top / 2,
)
writer.add_page(page3)
# add some Javascript to launch the print window on opening this PDF.
# the password dialog may prevent the print dialog from being shown,
# comment the the encription lines, if that's the case, to try this out:
writer.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
# write to document-output.pdf
with open("PyPDF2-output.pdf", "wb") as fp:
writer.write(fp)
页面旋转
最典型的旋转是将页面顺时针旋转 90 度的倍数。这是在页面方向错误时完成的。您可以使用 PageObject 类的旋转方法来做到这一点:
from PyPDF2 import PdfWriter, PdfReader
reader = PdfReader("input.pdf")
writer = PdfWriter()
writer.add_page(reader.pages[0])
writer.pages[0].rotate(90)
with open("output.pdf", "wb") as fp:
writer.write(fp)
rotate 方法通常优于 page.add_transformation(Transformation().rotate())
方法,因为 rotate
将确保页面仍在 mediabox / cropbox 中。 transformation 对象对页面内容的坐标进行操作,并且不会更改 mediabox 或 cropbox。
普通合并
from PyPDF2 import PdfReader, PdfWriter, Transformation
# Get the data
reader_base = PdfReader("labeled-edges-center-image.pdf")
page_base = reader_base.pages[0]
reader = PdfReader("box.pdf")
page_box = reader.pages[0]
page_base.merge_page(page_box)
# Write the result back
writer = PdfWriter()
writer.add_page(page_base)
with open("merged-foo.pdf", "wb") as fp:
writer.write(fp)
与旋转合并
from PyPDF2 import PdfReader, PdfWriter, Transformation
# Get the data
reader_base = PdfReader("labeled-edges-center-image.pdf")
page_base = reader_base.pages[0]
reader = PdfReader("box.pdf")
page_box = reader.pages[0]
# Apply the transformation
transformation = Transformation().rotate(45)
page_box.add_transformation(transformation)
page_base.merge_page(page_box)
# Write the result back
writer = PdfWriter()
writer.add_page(page_base)
with open("merged-foo.pdf", "wb") as fp:
writer.write(fp)
如果添加扩展参数:
transformation = Transformation().rotate(45)
page_box.add_transformation(transformation)
page_base.merge_page(page_box)
你将得到:
或者,您可以将合并后的图像向右移动一点,方法是使用
op = Transformation().rotate(45).translate(tx=50)
缩放
PyPDF2 提供了两种缩放方式:页面本身和页面上的内容。通常,您希望将两者结合起来。
缩放页面(画布)
from PyPDF2 import PdfReader, PdfWriter
# Read the input
reader = PdfReader("resources/side-by-side-subfig.pdf")
page = reader.pages[0]
# Scale
page.scale_by(0.5)
# Write the result to a file
writer = PdfWriter()
writer.add_page(page)
writer.write("out.pdf")
如果你希望有更多的控制,你可以直接调整各种页面框:
from PyPDF2.generic import RectangleObject
mb = page.mediabox
page.mediabox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page.cropbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page.trimbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page.bleedbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page.artbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
缩放内容
内容被缩放到坐标系的原点。通常,这是左下角。
from PyPDF2 import PdfReader, PdfWriter, Transformation
# Read the input
reader = PdfReader("resources/side-by-side-subfig.pdf")
page = reader.pages[0]
# Scale
op = Transformation().scale(sx=0.7, sy=0.7)
page.add_transformation(op)
# Write the result to a file
writer = PdfWriter()
writer.add_page(page)
writer.write("out-pg-transform.pdf")
更多建议: