[英]How can I stop Excel from displaying numbers in scientific notation using XlsxWriter?
I'm exporting data to XLSX using pandas, which in turn is using XlsxWriter
. 我正在使用pandas将数据导出到XLSX,而pandas又使用XlsxWriter
。 I've experimented with the formatting options I've found (eg float_format
, despite the fact that it seemed like the wrong answer based on the source), but I can't find anything that prevents Excel from interpreting numbers in scientific notation. 我已经尝试过使用找到的格式设置选项(例如float_format
,尽管事实似乎基于来源似乎是错误的答案),但是我找不到阻止Excel用科学计数法解释数字的任何内容。
For example, if I write 0.000000000001
, the underlying cell value in Excel is 0.000000000001
, but the display value is 1E-12
. 例如,如果我编写0.000000000001
,则Excel中的基础单元格值为0.000000000001
,但显示值为1E-12
。 I know that I can write the number as a string, but I'd like the value to be numeric in Excel. 我知道我可以将数字写为字符串,但是我希望该值在Excel中为数字。
You cannot stop Excel from displaying an unformatted number in whatever format it thinks is suitable. 您不能阻止Excel以其认为合适的格式显示无格式的数字。 The only way to guarantee the format is to specify a numerical format for the data. 保证格式的唯一方法是为数据指定数字格式。
Unfortunately there is currently no way to do this in XlsxWriter and or, as a result, in Pandas. 不幸的是,当前无法在XlsxWriter中或因此在Pandas中执行此操作。
A feature such as this is on the TODO list however it probably won't be available for a while. 诸如此类的功能已在TODO列表中,但是可能暂时无法使用。
As a workaround I recommend getting a reference to the underlying workbook and worksheet and overwriting any cells that you wish to be formatted with the same data from the Pandas dataframe and a XlsxWriter format. 作为一种解决方法,我建议您获得对基础工作簿和工作表的引用,并覆盖希望使用来自Pandas数据框和XlsxWriter格式的相同数据格式化的所有单元格。 I'll update later with an example of how to do that. 我将在后面的示例中进行更新。
Disclaimer: I wrote XlsxWriter and the Pandas/XlsxWriter integration code. 免责声明:我编写了XlsxWriter和Pandas / XlsxWriter集成代码。 The issue here is really to do with XlsxWriter limitations and not Pandas. 这里的问题实际上与XlsxWriter的限制有关,而不是与Pandas有关。
You can set the length of number and then use code like the following: 您可以设置数字的长度,然后使用如下代码:
import xlsxwriter
wb = xlsxwriter.Workbook('Test.xlsx')
ws = wb.add_worksheet('Num')
cell_format = wb.add_format({'num_format': '0.00000000000000'})
ws.write('A1', 0.000000000001, cell_format)
wb.close()
Just format the number when you specify in the write() method. 在write()方法中指定时,只需格式化数字即可。 Eg: 例如:
import xlsxwriter
wb = xlsxwriter.Workbook('myfile.xlsx')
ws = wb.add_worksheet('testme')
ws.write('A1', '%s' %123232323232323232435454546565)
wb.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.