【问题标题】:How to count the numer of pdf pages in python that has blank pdf page also如何计算python中也有空白pdf页面的pdf页面数
【发布时间】:2013-05-14 21:45:12
【问题描述】:

我尝试使用 pypdf 模块打印 pdf 文档的数量,其中包括一些空白的白色 pdf 页面。但它避免了空白页并打印剩余页数。下面是代码。

import sys

import pyPdf

from pyPdf import PdfFileReader, PdfFileWriter

pdf_document = PdfFileReader(file(normalpdfpath,"r"))

normal = pdf_document.getNumPages()
print normal

【问题讨论】:

    标签: python-2.7


    【解决方案1】:

    第 1 步:-

    pip 安装 pyPDF2

    第 2 步:-

    import requests, PyPDF2, io
    url = 'sample.pdf' 
    response = requests.get(url)
    with io.BytesIO(response.content) as open_pdf_file:
      read_pdf = PyPDF2.PdfFileReader(open_pdf_file)
      num_pages = read_pdf.getNumPages()
      print(num_pages)
    

    【讨论】:

    【解决方案2】:

    你可以试试这个,对我有用:

    import re
    import os
    
    rxcountpages = re.compile(r"/Type\s*/Page([^s]|$)", re.MULTILINE|re.DOTALL)
    
    def count_pages(filename):
        data = file(filename,"rb").read()
        return len(rxcountpages.findall(data))
    
    if __name__=="__main__":
        parent = "/Users/username/"
        os.chdir(parent)
        filename = 'LaTeX20120726.pdf'
        print count_pages(filename)
    

    问候

    【讨论】:

      猜你喜欢
      • 2022-12-06
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 2023-01-15
      • 1970-01-01
      相关资源
      最近更新 更多