python - 如何使用Python获取PDF文件元数据 'Page Size'?

标签 python scanning pypdf page-size

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

类似这样的:

import PyPDF2
pdf=PdfFileReader("sample.pdf","rb")
print(pdf.getNumPages())

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

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

pdf_info=pdf.getDocumentInfo()
print(pdf_info)

最佳答案

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

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

关于python - 如何使用Python获取PDF文件元数据 'Page Size'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46232984/

相关文章:

python - 递归提取数字

Golang : panic: runtime error: invalid memory address or nil pointer dereference using bufio. 扫描器

php - 如何在 PHP 中扫描

python - 将 PDF 转换/写入 RAM 作为类似文件的对象,以便进一步使用它

python - 检测循环中的最后一次迭代

Perl 正则表达式的 Python 版本

python - 为什么调用 .clear() 后字典大小为 72 字节,而实例化时为 240 字节?

c - 如何纠正扭曲的文档?

python-3.x - Python/PyPDF4 : How do I specify the/PageLabels in the created PDF?

python - 当 PyPDF2 正在解析的 PDF 损坏时,我可以让 PyPDF2 优雅地失败吗?