简体   繁体   English

如何使用python-xlsxwriter从excel文件中的单元格中删除绿色箭头?

[英]How can I remove the green arrow from a cell on excel file using python-xlsxwriter?

When i wrote a value that does have a percentage sign, excel shows me a green arrow on the top of that cell. 当我写一个有百分号的值时,excel会在该单元格顶部显示一个绿色箭头。

This is what I use to write a value in a specific cell. 这是我用来在特定单元格中写入值的方法。

worksheet.write(1, 46, '12%')

I tried this : 我试过这个:

worksheet.write_string(1, 46, '12%')

and this 还有这个

worksheet.write_number(1, 46, '12%')

but I get the same results. 但我得到了相同的结果。

How can i get rid of the green arrow? 我怎么能摆脱绿色箭头?

Thanks! 谢谢!

The green arrow/triangle is an Excel warning. 绿色箭头/三角形是Excel警告。 It is probably the warning about numbers stored as text. 这可能是关于存储为文本的数字的警告。

The way to avoid this is to write the number without the percentage and then format it with a number format so that the percentage sign is displayed. 避免这种情况的方法是写入没有百分比的数字,然后用数字格式对其进行格式化,以便显示百分号。 That is how it is generally done in Excel. 这就是它通常在Excel中完成的方式。

With XlsxWriter you can do it as follows: 使用XlsxWriter,您可以按如下方式执行此操作:

import xlsxwriter

# Create a new workbook and add a worksheet.
workbook = xlsxwriter.Workbook('percent.xlsx')
worksheet = workbook.add_worksheet()

# Create a percentage number format.
percent_format = workbook.add_format({'num_format': '0%'})

# Write a number as a percentage.
worksheet.write('A1', .12, percent_format)

workbook.close()

One way to do this is to write the cell as a formula rather than as a String. 一种方法是将单元格写为公式而不是字符串。 Eg in Apache POI, write as 例如,在Apache POI中,写为

row.createCell(col++).setCellFormula("\\"" + myValue + "\\""); row.createCell(col ++)。setCellFormula(“\\”“+ myValue +”\\“”);

rather than row.createCell(col++).setCellValue(myValue); 而不是row.createCell(col ++)。setCellValue(myValue);

I've done it this way in VBA scripts as well. 我也是这样在VBA脚本中完成的。 If this doesn't match your programming scenarios, well, basically if you go into Excel and put in '123 you'll see you get a green triangle. 如果这与你的编程场景不符,那么,基本上如果你进入Excel并输入'123,你会看到你得到一个绿色三角形。 But if you put in ="123" you don't. 但是,如果你输入=“123”,你就不会。 That's the behavior you need to replicate. 这就是你需要复制的行为。

Of course, you can just turn the error off. 当然,您可以关闭错误。 File-Options-Formulas-Numbers-Formatted as Text or preceded by an apostrophe. 文件选项 - 公式 - 数字 - 格式化为文本或以撇号开头。 But that's an Excel setting, not a workbook setting. 但这是一个Excel设置,而不是工作簿设置。 If you send the workbook to others and they have the option on, they will see those pesky triangles. 如果您将工作簿发送给其他人并且他们可以选择,他们将看到那些讨厌的三角形。 The Formula way is they way to go for consistency imho. Formula方式是他们实现一致性的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-xlsxwriter仅在使用csv数据时作为文本写入 - Python-xlsxwriter only writes as a text when using csv data 如何使用xlsxwriter将主题单元格样式添加到单元格? - How can I add Themed Cell Styles to a cell using xlsxwriter? 如何在python中使用xlsxwriter冻结excel文件中的行和列 - how to freeze a row and a columns in an excel file using xlsxwriter in python 如何使用xlsxwriter将文本文件转换为Excel? - How can make text file to excel using xlsxwriter? 如何在python中使用xlsxwriter从SQLITE导入数据到excel - how to import data from SQLITE to excel using xlsxwriter in python 如何使用XlsxWriter阻止Excel以科学计数法显示数字? - How can I stop Excel from displaying numbers in scientific notation using XlsxWriter? 使用 python 和 xlsxwriter 以另一种格式将新文本添加到 excel 单元格中 - Adding new text into excel cell with another format using python & xlsxwriter 如何使用 xlsxwriter 和 python 将图像添加到 xlsx 文件的标题中? - How can i add an image to a header of a xlsx file using xlsxwriter and python? 如何使用 xlsxwriter python package 创建多个自动过滤器 - How can I create multiple autofilter using xlsxwriter python package 如何一起使用 xlsxwriter 和 Excelwriter 来填充包含一些格式化文本以及 python 导出的 DataFrame 的 excel 文件? - How can I use xlsxwriter and Excelwriter together to populate an excel file which contains some formatted text as well as python exported DataFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM