Example of Python Code for Reading Text in a Docx File

Alternative approach:
Utilize the method that involves accessing the XML directly, instead of relying on the complex process of unpacking and re-packaging an Open Packaging Convention (OPC) package to make it compatible with Word. This method allows you to bypass the intricate saving details, as they can be handled by a separate tool.


Solution:

Experiment with this approach and the outcome will be a compilation of every continuous section in the document that falls under the category of

red

.

import docx
from docx.shared import RGBColor
def readtxt(filename):
    doc = docx.Document(filename)
    fullText = []
    for para in doc.paragraphs:
        for run in para.runs:
            if run.font.color.rgb == RGBColor(255, 000, 000):
                fullText.append(run.text)
    return fullText
fullText = readtxt('filepath.docx')

Ensure that the correct passing of

filepath

is being checked.

Frequently Asked Questions