【问题标题】:How to get PDF file metadata 'Page Size' using Python?如何使用 Python 获取 PDF 文件元数据“页面大小”?
【发布时间】:2018-02-24 06:10:26
【问题描述】:

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

类似这样的:

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

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

下面的这个命令打印某种元数据,但没有页面大小:

pdf_info=pdf.getDocumentInfo()
print(pdf_info)

【问题讨论】:

    标签: python scanning pypdf2 page-size


    【解决方案1】:

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

    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
    

    【讨论】:

    • ~0.352 正好是 25.4/72
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 2011-03-04
    相关资源
    最近更新 更多