title: Exporting data from Word tables to Excel using Python
body: <p>I have thousands of word documents that have one table on the first page with information that I need outputted into Excel. So far I have this, but I'm not sure why it's not working</p>
  
  <pre><code>import win32com.client as win32
  import os
  
  myDir = r'C:\Projects\Capital'
  
  
  XL = win32.Dispatch('Excel.Application')
  XL.Visible = 1
  XLbook = XL.Workbooks.Open(os.path.join(myDir,'Dealflow.xlsx'))
  XLsheet = XLbook.Worksheets(1)
  XLrow = 2
  
  for myFile in os.listdir(myDir):
      filepath = os.path.join(myDir,myFile)
      filename = os.path.splitext(myFile)[0]
      ext = os.path.splitext(myFile)[1]
      if ext == '.docx':
          word = win32.Dispatch('Word.Application')
          word.Visible = 1
          word.Documents.Open(filepath)
          doc = word.ActiveDocument
          table = doc.Tables(1)
          XLsheet.Cells(XLrow,1).Value = table.Cell(Row=1, Column=1).Range.Text
          XLsheet.Cells(XLrow,2).Value = table.Cell(Row=2, Column=3).Range.Text
          XLrow = XLrow +1
          doc.Close()
  </code></pre>