如何使用Python获取PDF文件元数据“页面大小”?

Mir*_*rek 5 python scanning pypdf page-size

我尝试在 Python 3 中使用 PyPDF2 模块,但无法显示“页面大小”属性。我想知道扫描为 PDF 文件之前纸张尺寸是多少。

像这样的东西:

import PyPDF2
pdf=PdfFileReader("sample.pdf","rb")
print(pdf.getNumPages())
Run Code Online (Sandbox Code Playgroud)

但我正在寻找另一个Python函数而不是例如getNumPages()...

下面的命令打印某种元数据,但不显示页面大小:

pdf_info=pdf.getDocumentInfo()
print(pdf_info)
Run Code Online (Sandbox Code Playgroud)

小智 7

这段代码应该可以帮助你:

import PyPDF2
pdf = PyPDF2.PdfFileReader("a.pdf","rb")
p = pdf.getPage(1)

w_in_user_space_units = p.mediaBox.getWidth()
h_in_user_space_units = p.mediaBox.getHeight()

# 1 user space unit is 1/72 inch
# 1/72 inch ~ 0.352 millimeters

w = float(p.mediaBox.getWidth()) * 0.352
h = float(p.mediaBox.getHeight()) * 0.352
Run Code Online (Sandbox Code Playgroud)