what if i am not able to read numbers in word documents using python?

Here is the code I wrote. My algorithm works only if your docx still has the same format (Questions: \n 1) … \n 2)… \n … \n Conclusions: 1)… \n 2)…\n …). For example if you put conclusions before questions it would not work.

I tried with the docx you provided and it works.

from docx2python import docx2python
import re

re_bullet = re.compile("[0-9]+\)") # integer followed by a parenthesis pattern

text = docx2python('test.docx').text #Docx to analyze



"""
    Count the number of questions contained into the text.
    Args : 
        text (str) : text to analyze 
    Returns :
        (int) : number of questions 
"""
def count_questions(text):
    result = 0
    lines= text.split("\n") #we split each lines
    for line in lines:
        if(line == "Conclusions:"): #if line contains "Conclusions:" then we stop to count questions.
            break

        if(re_bullet.match(line)): #if the line contains the bullet pattern then increment the result
            result+=1
    return result


"""
    Count the number of conclusions contained into the text.
    Args : 
        text (str) : text to analyze 
    Returns :
        (int) : number of conclusions 
"""
def count_conclusions(text):
    result = 0
    lines= text.split("\n")
    start_conclusion = False  #boolean to check if we are in the part which contains conclusions 
    for line in lines:
        if(line == "Conclusions:"):  #if line contains "Conclusions:" then we can start to count conclusions.
            start_conclusion = True

        if(re_bullet.match(line) and start_conclusion):
            result+=1
    return result


"""
    Check if there are as many questions as there are conclusions.
    Args : 
        text (str) : text to analyze 
    Returns :
        (boolean) : true if there are as many questions as there are conclusions, false if not.
"""
def questions_number_equals_to_conclusions_number(text):
    return count_questions(text) == count_conclusions(text)


print(str(questions_number_equals_to_conclusions_number(text)))

Here is the result :

True

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top