简体   繁体   中英

Numbers are being converted to date format automatically while writing in Excel sheet using python Openpyxl

I am doing an automation of one of my report. But when I am copying values from a list ie 'summary' to one of the sheet named 'Summary' in excel using openpyxl. It is automatically converting numbers into date. When I checked format cell in excel , cells are converted to custom field.

I tried code cell.number_format='General' . But it is also not working

'summary' is a list which i need to write in excel sheet named 'Summary'

>>>summary

[11, '19', 322, 2460, 317, 2413, 318, 4, 9, 47, 2370, 2370, 0, 0, '2455']

complete code which i am executing:

wb_obj=openpyxl.load_workbook('test.xlsx')

sheet_sumary=wb_obj['Summary']

max_col1 = sheet_sumary.max_column

m_row1 = sheet_sumary.max_row

for j in range(1,max_col1+1):

    cell=sheet_sumary.cell(row=m_row1+1,column=j)

    cell.value= (summary[j-1])

    cell.number_format='General'

Expected result in excel:

Date    Trace collection Hour   Total SMK Nodes Total SMK Cells Total Nodes TA Available    Total cells TA Available

3-Sep-19    19  322 2460    317 2413

Actual result in excel:

Date    Trace collection Hour   Total SMK Nodes Total SMK Cells Total Nodes TA Available    Total cells TA Available

11/Jan/00   19  17/Nov/00   25/Sep/06   12/Nov/00   9/Aug/06

Above is an example of some column and here we can see that my data is converted into some arbitrary date format like column 'Total SMK Nodes', whose value should be 322, but this number is converted to '17/Nov/00' while writing into excel.

This is a known PITA that Excel insists on inflicting on its users. There are a couple things you can do to mitigate the action.

  1. It looks like your Summary row example has a strange mix of int and str values. I'd suggest casting these to the appropriate values as you set the cells.
  2. Don't use the General format. If you want a number cell, use the actual Number formatting class. General is Excel's default and lets it do pretty much anything it wants.

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