from docx import Document
import pandas as pd
df = pd.read_csv('out.csv').to_dict()
word_doc = Document()
table_head = list(df.keys())
table_head_len = len(table_head)
tables_rows_len = len(df['name'])
# create tabel
table = word_doc.add_table(cols = table_head_len, rows = tables_rows_len)
# add the first row in the table
for i in range(table_head_len):
table.cell(row_idx = 0, col_idx = i).text = table_head[i]
# add rows for name col
for i in range(1, tables_rows_len):
table.cell(row_idx = i, col_idx = 0).text = df['name'][i]
# add rows for age col
for i in range(1, tables_rows_len):
table.cell(row_idx = i, col_idx = 1).text = str(df['age'][i])
word_doc.save('word_doc.docx')
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter