Search code examples
pythonpython-docx

Pandas table convert to word with repeated columns


I want the columns and the data arrange nicely as the below format in the word

Name: Sam
Age: 14
Weight: 45

Name:Andrea
Age: 25
Weight: 88

Name:Alex
Age: 55
Weight: 56

Name:Robin
Age: 8
Weight: 15

Name:Kia
Age: 21
Weight: 71

However,my code below unable to get teh desired output as the data was with the vertical output in table.

import pandas as pd
import docx

data = pd.DataFrame({'Weight':[45, 88, 56, 15, 71], 
                   'Name':['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'], 
                   'Age':[14, 25, 55, 8, 21]}) 

df = pd.DataFrame(data)
print(df.columns[0])
print(df.columns)
print(df)
# Initialise the Word document
doc = docx.Document()
# Initialise the table
t = doc.add_table(rows=df.shape[0]+1, cols=df.shape[1])
for j in range(df.shape[-1]):
    t.cell(0,j).text = df.columns[j]

   
# Add the body of the data frame to the table
for i in range(df.shape[0]):
    for j in range(df.shape[-1]):
        t.cell(i+1,j).text = str(df.values[i,j])
# Save the Word doc
doc.save(r'C:\Users\Downloads\table 1.docx')

Solution

  • You can convert your DataFrame to a list of recs with to_dict, then dump_all using :

    import yaml #pip install pyyaml
    
    order = ["Name", "Age", "Weight"]
    
    p = (yaml.dump_all(data[order].to_dict("records"),
        explicit_start=False, sort_keys=False).replace("-"*3, ""))
    
    doc = docx.Document()
    
    doc.add_paragraph(p); doc.save("file.docx")
    

    Output (file.docx) :

    enter image description here