简体   繁体   中英

Writing xls from python row wise using pandas

I have sucessfully created.xlsx files using pandas

df = pd.DataFrame([list of array])

'''
:param data: Data Rows
:param filename: name of the file
:return:
'''
df = pd.DataFrame(data)

# my "Excel" file, which is an in-memory output file (buffer)
# for the new workbook
excel_file = BytesIO()

writer = pd.ExcelWriter(excel_file, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1_test')

writer.save()
writer.close()

# important step, rewind the buffer or when it is read() you'll get nothing
# but an error message when you try to open your zero length file in Excel
excel_file.seek(0)

# set the mime type so that the browser knows what to do with the file
response = HttpResponse(excel_file.read(),
                        content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

# set the file name in the Content-Disposition header
response['Content-Disposition'] = 'attachment; filename=' + filename + '.xlsx'

return response

But I have issue here, There is unnecessary SNo. which i dont want, how to do I remove that.

有 SNo。作为第一行和第一列,我如何删除它?

There is SNo. as first row and column, How do i remove that?

according to the documentation here

to_excel default set index to write as a new column, use index as False

df.to_excel(writer, sheet_name='Sheet1_test',index=False)

You can take reference from this https://medium.com/better-programming/using-python-pandas-with-excel-d5082102ca27 post of medium for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM