from docx import Document
import openpyxl
# Open the Word document
doc = Document('path_to_word_document.docx')
# Create a new section or paragraph to insert the Excel spreadsheet
section = doc.sections[0]
new_paragraph = section.add_paragraph()
# Load the Excel spreadsheet
workbook = openpyxl.load_workbook('path_to_excel_file.xlsx')
worksheet = workbook.active
# Loop through the rows of the Excel spreadsheet
for row in worksheet.iter_rows():
# Create a new table row in the Word document
table_row = new_paragraph.add_run().add_table(1, len(row))
table_row.style = 'Table Grid'
# Fill the table row with cell values from Excel
for cell in row:
table_cell = table_row.cell(0, 0)
table_cell.text = str(cell.value)
# Save the Word document with the inserted Excel spreadsheet
doc.save('path_to_updated_word_document.docx')