如何提取docx的特定列并将其保存到python中的dataframe?

b91juud3  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(174)

我在docx中有以下两个表


# Table one

  item components Description  qty  

   1    laptop      asus        2

        processor   i5-1135G7 
                    Processor 

        ram         8 GB DDR4 3200MHz 

  #Table 2

  item components Description  qty  

   1    battery      lethium 
                     ion  

        gpu         rtx 2060 super 

  #Table 'n'

我只想从所有n个表中提取描述。
到目前为止,我已经尝试了以下代码:

document = Document('test.docx')
tables = document.tables

tables = []
for table in document.tables:
    df = [['' for i in range(len(table.columns))] for j in range(len(table.rows))]
    for i, row in enumerate(table.rows):
        for j, cell in enumerate(row.cells):
            if cell.text:
                df[i][j] = cell.text
    tables.append(pd.DataFrame(df))

table:

item components Description  qty 

   1    laptop      asus        2

        processor   i5-1135G7 
                    Processor 

        ram         8 GB DDR4 3200MHz 

  item components Description  qty  

   1    battery      lethium 
                     ion  
        gpu         rtx 2060 super 

  #rest of other tables

我希望所有表都以描述为标题排列到dataframe中。预期产出:

Description

   i5-1135G7 
   Processor 
   8 GB DDR4 3200MHz     
   lethium ion  
   rtx 2060 super

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题