简体   繁体   English

如何使用XlsxWriter阻止Excel以科学计数法显示数字?

[英]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.

相关问题 如何防止 excel 将 int 转换为科学计数法? - How can I prevent excel from converting int into scientific notation? 如何避免 python 将大数转换为科学计数法? - How can I avoid python converting big numbers to scientific notation? 当我从 Pandas 的 excel 中读取十六进制字符串时,如何抑制 python 中的科学记数法? - How do I suppress scientific notation in python when I read hexadecimal string from excel by pandas? 如何使用python-xlsxwriter从excel文件中的单元格中删除绿色箭头? - How can I remove the green arrow from a cell on excel file using python-xlsxwriter? 如何在 Matplotlib 金融上抑制科学记数法? - How can i suppress scientific notation on Matplotlib finance? 在matplotlib中用“X”代替“e”科学记数法显示数字 - Displaying numbers with “X” instead of “e” scientific notation in matplotlib 如何获得大量非科学符号? - How can I get a big number in non-scientific notation? 将数字转换为科学计数法 - Convert numbers to scientific notation 如何防止/禁止 Decimal 使用科学记数法? - How to prevent/suppress Decimal from using scientific notation? 如何遍历未使用 XlsxWriter 显式命名的 Excel 工作表? - How can I iterate through Excel Worksheets that aren't explicitly named using XlsxWriter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM