Count Words, Pages, Paragraphs & Lines in Word with Python

Casie LiuCasie Liu
4 min read

Counting words, paragraphs, lines, and other elements in Word documents is essential in tasks like editing, academic writing, and publishing. Whether you're meeting a minimum word count for a dissertation or keeping paragraphs concise for better readability, having accurate document stats is crucial. With Python, you can automate these checks and quickly retrieve detailed information—saving time and ensuring your content meets formatting or publishing standards.

Python Library to Count Elements in Word Documents

To better accomplish this task, I recommend using Python libraries that simplify Word document analysis. Open-source options like python-docx allow you to count words in entire documents or specific paragraphs. Another choice, pywin32, also supports word processing tasks but works only on Windows. If you're looking for more advanced features—like extracting formatted paragraphs, finding and replacing text, or generating a table of contents—enterprise libraries such as Spire.Doc or Aspose.Words are more powerful.

While different libraries may have varying syntax, the overall logic remains similar. In this guide, I’ll demonstrate how to count words and paragraphs using Spire.Doc due to its simplicity and rich feature set. You can install it easily with a pip command: pip install Spire.Doc.

Count Words, Pages, Characters, Paragraphs & Lines in a Word Document

Once everything is set up, let’s dive into the main task—counting words, pages, characters, paragraphs, and more in an entire Word document. For things like journal submissions or dissertations, page or word limits are common. While you can check these counts manually from the bottom corner of MS Word, that’s not practical when dealing with multiple files. Luckily, Python makes it easy to handle this in bulk. With the Document.BuiltinDocumentProperties property in Spire.Doc, you can retrieve word counts and other stats without even opening the document. Here’s how to do it step by step.

Steps to count words, paragraphs and more in a Word document:

  • Create a Document object.

  • Read a source Word document from files using the Document.LoadFromFile() method.

  • Create a list.

  • Access the properties through the Document.BuiltinDocumentProperties property.

  • Append the information to the list and save it as a Text file.

      from spire.doc import *
      from spire.doc.common import *
    
      # Create an object of the Document class
      doc = Document()
      # Load a Word document
      doc = Document("/AI-Generated Art.docx")
    
      # Create a list
      sb = []
    
      # Get the built-in properties of the document
      properties = doc.BuiltinDocumentProperties
    
      # Get the number of words, characters, paragraphs, lines, and pages and append the result to the list
      sb.append("The number of words: " + str(properties.WordCount))
      sb.append("The number of characters: " + str(properties.CharCount))
      sb.append("The number of paragraphs: " + str(properties.ParagraphCount))
      sb.append("The number of lines: " + str(properties.LinesCount))
      sb.append("The number of pages: " + str(properties.PageCount))
    
      # Save the data in the list to a text file
      with open("/countresult.txt", "w") as file:
          file.write("\n".join(sb))
    
      doc.Close()
    

    Python Count Words Paragraphs Characters and More in Word

    Count Words and Characters in a Paragraph of Word Documents

    In the previous chapter, I showed how to retrieve properties like word and paragraph counts for the entire Word document. But what if you only need to analyze a specific paragraph? You can access it using the Sections[].Paragraphs[] property, then get its word and character counts with Paragraph.WordCount and Paragraph.CharCount properties. Let’s walk through the steps and check out the code examples!

    Steps to count words and characters in a paragraph of a Word document:

    • Create an instance of the Document class.

    • Read a Word file through the Document.LoadFromFile() method, and access the specified paragraph using the Sections[].Paragraphs[] property.

    • Retrieve the word counts with the Paragraph.WordCount property, and the character counts with the Paragraph.CharCount property.

    • Append the data to a list and then save the list as a Text file.

The code example here shows how to count words and characters in the second paragraph of a Word file:

    from spire.doc import *
    from spire.doc.common import *

    # Create an object of the Document class
    doc = Document()
    # Load a Word document
    doc = Document("/AI-Generated Art.docx")

    # Get the second paragraph
    paragraph = doc.Sections[0].Paragraphs[1]

    # Create a list
    sb = []

    # Get the number of words and characters in the paragraph and append the result to the list
    sb.append("The number of words: " + str(paragraph.WordCount))
    sb.append("The number of characters: " + str(paragraph.CharCount))

    # Save the data in the list to a text file
    with open("/countresult-para.txt", "w") as file:
        file.write("\n".join(sb))
    doc.Close()

Python Count Words and Characters in a Paragraph of a Word Document

The Bottom Line

By the end of the blog, you can efficiently retrieve word, character, paragraph, and page counts in Word documents—either globally or within specific sections. Leveraging Python libraries allows you to automate these tasks and integrate them into larger document processing workflows. Hopefully, this guide provides a clear reference for implementing word count features in your Python projects.

0
Subscribe to my newsletter

Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Casie Liu
Casie Liu