5

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

import sys

import pyPdf

from pyPdf import PdfFileReader, PdfFileWriter

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

normal = pdf_document.getNumPages()
print normal
4

2 回答 2

2

步骤1:-

点安装 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)
于 2019-11-20T13:23:40.687 回答
1

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

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)

问候

于 2013-05-20T12:43:56.090 回答