0

Normally can do this for dataframes without multi index:

import docx

df = pd.DataFrame(data)
doc = docx.Document('./test.docx')

t = doc.add_table(df.shape[0]+1, df.shape[1])

for j in range(df.shape[-1]):
    t.cell(0,j).text = df.columns[j]

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])

doc.save('./test.docx')

The problem arises with multi index in DataFrame the output is empty. How to resolve this issue?

JaySabir
  • 322
  • 1
  • 10
  • 1
    https://stackoverflow.com/questions/54532087/how-to-write-groupby-grouped-table-from-dataframe-to-word-document - Does this help ? – Joe Ferndz Feb 17 '21 at 20:31
  • I dont know how pandas support docx IO with `MultiIndex`, but you still can use `df.reset_index()` before saving your DataFrame, and manually set the `MultiIndex` when reading it. – LucasG0 Feb 17 '21 at 21:19

0 Answers0