Is there any way I can count the number of pages of a word document in python?

102 Views Asked by At

I am asked to count the number of pages of a word document without converting it to a pdf type.

I tried 'counting the sections' method to count the number of pages, but that method proved to be inaccurate, I need to accurately count the number of pages of word document using python-docx library.

Update - Since the documents wouldn't have anything unique to it, counting the pages using a place holder is not possible, the only unique thing to each page will be its page number as its footer.

1

There are 1 best solutions below

2
Tanveer On

Install the python-docx library. You can install it like this:

pip install python-docx

Here is the code:

from docx import Document


def count_pages(doc):
    page_count = 0
    for para in doc.paragraphs:
                 # Microsoft Word 2010 - Level 2 is an text find at the end of the page.
                 # so on the base of it, i count total number of pages. 
        if para.text == 'Microsoft Word 2010 - Level 2':
            page_count += 1
    return page_count


doc_path = 'test.docx'
doc = Document(doc_path)
num_pages = count_pages(doc)

print(f'This document has {num_pages} pages.')

enter image description here