title: Extracting docx tables to excel
body: <p>I am trying to extract tables inside several word files at once and then save it to excel. It might be a silly job for those who know how to do it.</p>
  <p>I am able to do it one by one using the code bellow. However, I would like to do run the code once, extract the tables from each word file and save them in excel without having to run the code for each of the files.</p>
  <p>Many thanks for any guidance you are able to provide.</p>
  <p>Posting the code here:</p>
  <pre><code>from docx.api import Document
  import pandas as pd
  
  path=(r'/Users/folder/file.docx')
  document=Document(path)
  output_path=(r'/Users/folder/')
  writer=pd.ExcelWriter('{}/file.xlsx'.format(output_path),engine='xlsxwriter')
  
  i=0
  table_count=len(document.tables)
  for i in range(i,table_count):
      table=document.tables[i]
      data=[]
      keys=None
      row_data=None
      for j, row in enumerate(table.rows):
          text=(cell.text for cell in row.cells)
          if j==0:
              keys=tuple(text)
              continue
          row_data=dict(zip(keys,text))
          data.append(row_data)
      df=pd.DataFrame(data)
      df.to_excel(writer,sheet_name='table_{}'.format(i),index=False)
  writer.save()
  </code></pre>