问题描述
我编写了一个脚本,以便将我的所有.odt文件递归地转换为CWD和所有子目录中的文本文件。有问题的代码:
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)
当我运行它时,出现以下错误:
文件“ ./odtR.py”,第12行,在 allparas = textdoc.getElementsByType(text.P) AttributeError:“ str”对象没有属性“ 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)
在以前的脚本中我做了什么错?您将如何重写它?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)