I am new to Python. I tried writing a code to extract details from specific Word's table cell and export as Excel. However, I seems can't to extract the correct details from word's table to excel sheet. I am able to extract details from row 1-3, but not for other rows
The following is the word table:
https://i.stack.imgur.com/0rQZS.png[
^]
What I have tried:
import os
import xlsxwriter
import xlwt
import docx
from docx import Document
doc = Document ('/Users/TP/approach/data/sample.docx')
path = "/Users/TP/approach/data/"
files = os . listdir ( path )
docx_list = [ ]
for f in files :
if os . path . splitext ( f ) [ 1 ] == '.docx' :
docx_list . append ( path + '//' + f )
else :
pass
tb = doc.tables
rows = tb[0].rows
cols = rows[0].cells
cell = cols [ 0 ]
text = cell . text
`
mat = [ ]
for a in range ( len ( docx_list ) ) :
doc = Document ( docx_list [ a ] )
tb = doc . tables [ 0 ]
row = []
for i in range ( 1 , 7 , 6 ) :
cell = tb . cell ( 1 , i )
txt = cell . text if cell . text != '' else ''
row . append ( txt )
for l in range ( 1 , 7 , 6 ) :
cell = tb . cell ( 4 , l )
txt = cell . text if cell . text != '' else ''
row . append ( txt )
for m in ( 1, 7 , 6 ) :
cell = tb . cell ( 5 , m )
txt = cell . text if cell . text != '' else ''
row . append ( txt )
for n in range ( 2 , 7 , 1) :
cell = tb . cell ( 6 , n )
txt = cell . text if cell . text != '' else ''
row . append ( txt )
for o in range ( 3 , 7, 1) :
cell = tb . cell ( 7 , o )
txt = cell . text if cell . text != '' else ''
row . append ( txt )
workbook = xlsxwriter.Workbook('/Users/TP/missed_approach/output/missed approach_output_7.xlsx')
xlsheet = workbook.add_worksheet('data')
table_head = [' date ', 'time','aircraft_call_sign','runway_in_use','aircraft_type','persons_on_board','name_of_airline','point_of_depature','aircraft_registration','destination','reason']
headlen = len(table_head)
for i in range(headlen):
xlsheet.write(0,i,table_head[i])
for i in range(len(mat)):
for j in range(len(row)):
xlsheet.write(i+1,j,mat[i][j])
workbook.close()