How can I get the total count of total pages of a pdf using pdfminer in python

后端 未结 4 466
旧巷少年郎
旧巷少年郎 2021-02-06 17:10

In PyPDF2 pdfreader.getNumPages() gives me the total number of pages of a pdf file.

How can I get this using pdfminer?

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-06 17:36

    Using pdfminer,import the necessary modules.

    from pdfminer.pdfparser import PDFParser
    from pdfminer.pdfdocument import PDFDocument
    from pdfminer.pdfpage import PDFPage
    

    Create a PDF parser object associated with the file object.

    fp = open('your_file.pdf', 'rb')
    parser = PDFParser(fp)
    

    Create a PDF document object that stores the document structure.

    document = PDFDocument(parser)
    

    Iterate through the create_pages() function incrementing each time there is a page.

    num_pages = 0
    for page in PDFPage.create_pages(document):
        num_pages += 1
    print(num_pages)
    

提交回复
热议问题