John Smith
John Smith

Reputation: 897

AttributeError: 'str' object has no attribute 'P' when trying to extract text from all .odt files, recursively, using odfpy library

I wrote a script in order to convert all my .odt files, recursively - that is in the CWD and all the subdirectories, to text files. The code in question:

import glob, os
from odf import text, teletype
from odf.opendocument import load

fileList = glob.glob(f"{os.getcwd()}/**/*.odt", recursive=True) 

for f in fileList:
    textdoc = load(f)
    allparas = textdoc.getElementsByType(text.P) 
    print(allparas)
    s = len(allparas)
    text = ""
    for i in range(s):
        text += teletype.extractText(allparas[i])
        text += "\n"

    output_file = f.replace(".odt", "")
    with open(output_file, 'w') as textfile:
        textfile.write(text)

When I run it I get the following error:

File "./odtR.py", line 12, in allparas = textdoc.getElementsByType(text.P) AttributeError: 'str' object has no attribute 'P'

By comparison, all is fine when I run a similar script which is meant to convert just one file of my choosing from CWD . This is the code of this script:

from odf import text, teletype
from odf.opendocument import load

path_to_your_odt_file = input("What is the name of your odt file?\n")


output_file = path_to_your_odt_file.replace(".odt", "")


textdoc = load(path_to_your_odt_file)
allparas = textdoc.getElementsByType(text.P) 
s = len(allparas)
text = ""
for i in range(s):
    text += teletype.extractText(allparas[i])
    text += "\n"
    
output_file = path_to_your_odt_file.replace(".odt", "")
with open(output_file, 'w') as textfile:
    textfile.write(text)

What did I do wrong in the former script? How would you rewrite it?

Upvotes: 0

Views: 483

Answers (0)

Related Questions