The python code to insert a table between two paragraph of a word document is not functionning

Hi there,
Could you kindly help me correct my python code that has failed to insert a table between two paragraphs of a word document. I attach the python code used and the output.

output:
top paragraph
bottom paragraph
table

my wish is to obtain this output:

top paragraph
table
bottom paragraph

the code used is as follows:

import pandas as pd
from docx import Document
from docx.shared import Pt

# Function to extract data from Excel and create Word document
def create_word_documents(excel_path, word_template_path, output_folder):
    # Read Excel file
    df = pd.read_excel(excel_path)

    # Group data by first names
    grouped_data = df.groupby('First name')

    # Iterate over groups and create a Word document for each individual
    for name, group in grouped_data:
        # Load the Word template
        template_doc = Document(word_template_path)

        # Add a heading with the individual's name
        heading = template_doc.add_paragraph()
        run = heading.add_run(f"Individual: {name}")
        
        # Set the heading font size (adjust as needed)
        font = run.font
        font.size = Pt(14)

        # Add a table with the individual's data
        table = template_doc.add_table(rows=1, cols=len(df.columns))
        table.autofit = False

        # Add column headers
        for col_num, col_name in enumerate(df.columns):
            table.cell(0, col_num).text = col_name

        # Add data rows
        for _, row in group.iterrows():
            new_row = table.add_row()
            for col_num, value in enumerate(row):
                new_row.cells[col_num].text = str(value)

        # Save the Word document
        output_file_path = f"{output_folder}\\{name}_document.docx"
        template_doc.save(output_file_path)

if __name__ == "__main__":
    # Specify the paths for the Excel, Word template, and output files
    excel_file_path = r'C:\Users\mwo011082\Desktop\essaie\data.xlsx'
    word_template_path = r'C:\Users\mwo011082\Desktop\essaie\template.docx'
    word_output_folder = r'C:\Users\mwo011082\Desktop\essaie'

    create_word_documents(excel_file_path, word_template_path, word_output_folder)

If you need the template used for word document and excel document for data, I will be happy to provide

Thank you in advance

I assume this is the part adding the “top paragraph”.

I assume this is the part adding the table.

But I can’t see anything that would add a “bottom paragraph” at all. What part of the code do you expect to do this part, and how do you expect it to work?

Is the “top paragraph” supposed to come from the template, perhaps? Or maybe the “bottom paragraph” is?