【问题标题】:How to get Total Number of Pages of a PDF using Ghostscript and Python如何使用 Ghostscript 和 Python 获取 PDF 的总页数
【发布时间】:2023-02-12 00:16:33
【问题描述】:

我没有找到一个 python 兼容的脚本来使用 python 和 Ghostscript 来计算 PDF 中的页数。 我想将页数保存在 python 变量中。 命令行脚本已经存在,但我想将它与 python 一起使用。 如果你找到了,请告诉我。

【问题讨论】:

    标签: python pdf ghostscript


    【解决方案1】:

    最初,我想做同样的事情:使用ghostscript 来计算 pdf 文件中的页数。但是,我遇到了另一个名为 pypdf 的库,最终我决定改用它。以下函数返回传入路径中可用 pdf 的页数。我希望这有帮助。

    import pypdf
    
    def num_of_pages_for_pdf(pdf_file_path):
        """Returns the page count of the pdf available at the input path.
        
            Parameters:
                pdf_file_path (str): Local path of the pdf. E.g. '/tmp/example.pdf'
        """
        pdf_reader = pypdf.PdfReader(pdf_file_path)
        return len(pdf_reader.pages)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2015-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多