簡體   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