I am using pandas to read and write excel files with python (xlsx files, using openpyxl). Part of my data is text that looks like numbers. For instance, in the original excel file, I have a cell
and it imports fine as text in python. However, if I write the excel file again, the output cell has format ‘General’ and shows as
So my question is: Is there any way to set the number format in pandas? I would like to set it to ‘@’, i.e., to
Unfortunately, the documentation of pandas.ExcelWriter is not so advanced (i.e., it does not exist).
Thank you for your help.
I think in the worst case you can manipulate cells directly. I don’t have Excel installed but maybe you can check if this works.
In : df=pd.DataFrame () In : writer = pd.ExcelWriter ('e.xlsx', engine='openpyxl') In : df.to_excel (writer, 'sheet1') In : ws = writer.sheets['sheet1'] In : ws['A1'].style.number_format.format_code='@' In : writer.save () In : pd.read_excel ('e.xlsx') Out: 0 0 123456789012345