【发布时间】:2023-02-12 00:16:33
【问题描述】:
我没有找到一个 python 兼容的脚本来使用 python 和 Ghostscript 来计算 PDF 中的页数。 我想将页数保存在 python 变量中。 命令行脚本已经存在,但我想将它与 python 一起使用。 如果你找到了,请告诉我。
【问题讨论】:
标签: python pdf ghostscript
我没有找到一个 python 兼容的脚本来使用 python 和 Ghostscript 来计算 PDF 中的页数。 我想将页数保存在 python 变量中。 命令行脚本已经存在,但我想将它与 python 一起使用。 如果你找到了,请告诉我。
【问题讨论】:
标签: python pdf ghostscript
最初,我想做同样的事情:使用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)
【讨论】: