Home > front end >  Is it possible to print a statement to the user when a list index out of range
Is it possible to print a statement to the user when a list index out of range

Time:04-24

I am working on some code (as shown below) to extract the footer and header from a Word document. I am using python 3.10.4 and the python-docx library (version 0.8.1.1) on PyCharm (Community Edition 2021.3.3). Only the footer and header from the second section is extracted. This works as along as the document has two sections (created via 1x Section Break (Next Page) ).

If a document that does not have a minimum of two sections is accessed by the code, an error message is displayed by the console. This message is as follows: IndexError: list index out of range. This is as expected as the document does not have the required number of sections for the code to execute properly.

I would like to know if it is possible to print a message (e.g. "Doc has less than 2 sections") to the user in the instance that the document causes such an error. I have tried to use the len function to check if the variable "section2" is empty and if it is print a statement about the wrong number of sections in the doc. But this approach produces the following error message: TypeError: object of type 'Section' has no len().

Any form of help would be appreciated. If there are any questions about the code, please ask.

from docx import Document

# obtain file name from user
filename = input("Enter file name (e.g. my_file.docx): ")
print()
# access Word document file
WordFile = Document(filename)

# access the 2nd section
# [1] refers to the second section of the Word doc
section2 = WordFile.sections[1]

# loop through the 2nd section and print the header
header2 = section2.header
section2header = sorted(set())
for paragraph in header2.paragraphs:
    for run in paragraph.runs:
        # check for duplicates and append unique values to the sorted list section2header
        if paragraph.text not in section2header:
            section2header.append(paragraph.text)
print("Section 2 Header:", section2header)

# loop through the 2nd section and print the footer
footer2 = section2.footer
section2footer = sorted(set())
for paragraph in footer2.paragraphs:
    for run in paragraph.runs:
        # check for duplicates and append unique values to the sorted list section2footer
        if paragraph.text not in section2footer:
            section2footer.append(paragraph.text)
print("Section 2 Footer:", section2footer)

CodePudding user response:

Perhaps a try-except block is what you desire? (link to try-except) Then, once the error occurs (out of range), the except block is triggered, in this case, it prints an error message to the user. For example:

try:
    # any bit of code that causes the out of range error
except:
    print("Hey user, an error occured!")

The except block should catch when the error occurs and then execute the code in its body.

If you'd like it to catch just your specific error, try this:

try:
    # any bit of code that causes the out of range error
except IndexError as error:
    print("Hey user, an error occured!")

CodePudding user response:

Apparently you tried to use len(section2), but in order to get the number of sections you should be able to use len(WordFile.sections).

If in section2 = WordFile.sections[1] there is no second section, the section2 will not be "empty", but it won't exist at all.

  • Related