我编写了一个脚本,以递归方式将CWD和所有子目录中的所有.odt文件转换为文本文件。有关守则:
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)
当我运行它时,我得到以下错误:
File "./odtR.py", line 12, in allparas = textdoc.getElementsByType(text.P) AttributeError: 'str' object has no attribute 'P'
相比之下,当我运行一个类似的脚本,只转换我从CWD中选择的一个文件时,一切都很好。以下是此脚本的代码:
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)
前一个剧本我做错了什么?你将如何重写它
目前没有回答
相关问题 更多 >
编程相关推荐