11

I would like to print out a dataframe in Excel. I am using ExcelWriter as follows:

writer = pd.ExcelWriter('test.xlsx')
df = DataFrame(C,ind)    # C is the matrix and ind is the list of corresponding indices 
df.to_excel(writer, startcol = 0, startrow = 5)
writer.save()

This produces what I need but in addition I would like to add a title with some text (explanations) for the data on top of the table (startcol=0 ,startrow=0).

How can I add a string title using ExcelWriter?

Fabio Lamanna
  • 20,504
  • 24
  • 90
  • 122
Hamed
  • 757
  • 3
  • 10
  • 17

2 Answers2

18

You should be able to write text in a cell with the write_string method, adding some reference to XlsxWriter to your code:

writer = pd.ExcelWriter('test.xlsx')
df = DataFrame(C,ind)    # C is the matrix and ind is the list of corresponding indices 
df.to_excel(writer, startcol = 0, startrow = 5)

worksheet = writer.sheets['Sheet1']
worksheet.write_string(0, 0, 'Your text here')

writer.save()
Fabio Lamanna
  • 20,504
  • 24
  • 90
  • 122
  • Thanks a million Fabio. I tried to use write_string but got some errors for wrong implementation. Now it works perfectly. – Hamed Jan 13 '16 at 13:35
6

This will do the trick:

In[16]: sheet = writer.sheets['Sheet1'] #change this to your own
In[17]: sheet.write(0,0,"My documentation text")
In[18]: writer.save()
Yannis P.
  • 2,745
  • 1
  • 24
  • 39