Spaces:
Sleeping
Sleeping
File size: 835 Bytes
53629a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | from docx import Document
def extract_first_table_as_dict(doc_path):
# Load the Word document
doc = Document(doc_path)
# Ensure there's at least one table
if not doc.tables:
return []
# Get the first table
table = doc.tables[0]
# Assume first row is the header
keys = [cell.text.strip() for cell in table.rows[0].cells]
# Extract remaining rows as dictionary entries
data = []
for row in table.rows[1:]:
values = [cell.text.strip() for cell in row.cells]
# Make dictionary from keys and values
row_dict = dict(zip(keys, values))
data.append(row_dict)
return data
if __name__ == "__main__":
doc_path = "output.docx"
table_data = extract_first_table_as_dict(doc_path)
print(table_data) |