1
row = 5  
column = 0
writer = pd.ExcelWriter(file_name, engine='openpyxl')    
response = send_request('2017-2018-regular', item).content
df = pd.read_csv(io.StringIO(response.decode('utf-8')))
df.to_excel(writer, sheets, startrow=row, startcol=column, index=False)

I would like to put a simple title at the top of my Excel sheet in considering I am working with pandas and openpyxl. How could I do such thing? I want that title could be displayed on the top of the sheet (startrow=0, startcol=0). Please show me an example how to use it.

I know the question Write dataframe to excel with a title is related, but I can't use it for the simple reason that the engine is different. I use openpyxl lib and they used xlsxwriter lib in their answer. What is the equivalent for write_string, but with pandas?

enter image description here

  • What's your question, how to insert a title, or what's the equivalent of `write_string` in `openpyxl`? Also, I'm not sure, but is `openpyxl` or `pandas` column/rows in Excel `0` based? In Excel there's no column 0. You need to start at `1`. Perhaps that's the issue here? – BruceWayne Oct 16 '17 at 04:28

1 Answers1

1

well in openpyxl first row/column start with 1 instead of 0 so row=1,column=1 will be first (0,0) top-left cell where you need to start writing

check following example.

from openpyxl import Workbook

wb = Workbook()
dest_filename = 'empty_book.xlsx'

ws1 = wb.active #first default sheet if you want to create new one use wb.create_sheet(title="xyz")
ws1.title = "Title set example"

for col in range(1, 10):
    ws1.cell(column=col, row=1, value="Title_{0}".format(col))
wb.save(filename = dest_filename)
DexJ
  • 1,264
  • 13
  • 24