代码如下,经查看页面尺寸,左下角显示的是A4尺寸
但用python的PyPDF2包读取时,发现奇怪现象。这些A4页面均读取为A3尺寸。以下是将两A4页面(其中一页会读取为A3)合并后,读取的情况
import PyPDF2
def GetPdfsize(filepath):
"""
根据传入的pdf文件路径,返回该文件的尺寸(长和宽),较小的值在左。注意要判断只接收pdf文件
:param filepath:
:return: 列表形式返回文件内页面尺寸,每一页尺寸一个元组,是列表中的一项。
"""
list_size = []
pdf = PyPDF2.PdfReader(filepath)
for page in pdf.pages:
w = float(page.mediabox.width) * 0.352
h = float(page.mediabox.height) * 0.352
# 一律设置尺寸两数值的左边为较小值。
if w > h:
w, h = h, w
list_size.append((round(w), round(h)))
print(list_size) # [(315, 446)]
return list_size
# 打开PDF文件
with open(r"C:\Users\xcxc\Desktop\A3A4.pdf", 'rb') as file:
reader = PyPDF2.PdfReader(file)
# 遍历每一页
for page in reader.pages:
# 获取页面的宽度和高度
width, height = page.mediabox.width, page.mediabox.height
print(f"页面尺寸: 宽度 = {width}, 高度 = {height}") # Page Size: Width = 1267.41, Height = 896.203
GetPdfsize(r"C:\Users\xcxc\Desktop\A3A4.pdf")
'''
页面尺寸: 宽度 = 1267.41, 高度 = 896.203
页面尺寸: 宽度 = 595.276, 高度 = 841.89
[(315, 446), (210, 296)]
'''