繁体   English   中英

遍历docx的文件列表以提取和处理表

[英]iterate through file list of docx to extract and process table

我在几个目录和子目录中面临 3000 个 docx。 我必须准备一个列表,其中包含文件名和从 docx 中的表中提取的信息。 我已成功将所有 docx 添加到列表targets_in_dir中,将其与不相关的文件分开。

问题:我想遍历targets_in_dir从 docx 中提取所有表,

len_target =len(targets_in_dir)
file_processed=[]
string_tables=[]

for i in len_target:

    doc = docx.Document(targets_in_dir[i])
    file_processed.append(targets_ind[i])

    for table in doc.tables:
        for row in table.rows:
            for cell in row.cells:
                str.split('MANUFACTURER')
                string_tables.append(cell.text)

我收到错误'int' object is not iterable

 ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-4847866a9234> in <module>
      4 string_tables=[]
      5 
----> 6 for i in len_target:
      7 
      8     doc = docx.Document(targets_in_dir[i])

TypeError: 'int' object is not iterable

我究竟做错了什么?

看起来您正在尝试遍历len_target = len(targets_in_dir) ,这是一个 int。 因为int不是可迭代的 object,所以您的 for 循环失败。
您需要遍历可迭代的 object 才能使for循环正常工作。
将其固定为

for i in range(len_target):
    # do stuff

或者

for i in targets_in_dir:
    # do stuff

是一个很好的起点。

此外,您的file_processed.append(targets_ind[i])有错字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM