我们有一个需要处理Excel电子表格的应用程序。其中一些带有疯狂的数字格式,因此像1.23456789这样的数字显示为1.23。我们需要自动删除此格式,并且只删除此格式。正确的方法似乎是使用pythonwin32comapi编写一个程序,让Excel打开一个电子表格,然后将带有数字的单元格的格式更改为通用格式。
有什么简单的方法?
下面是一个简单的代码片段,它循环遍历excel工作簿中的所有单元格,并有条件地更新每个单元格的数字格式。它使用 xlwings
xlwings
import xlwings as xw book = xw.Book('example.xlsx') for sheet in book.sheets: for cell in sheet.used_range: if cell.number_format == '0.00': cell.number_format = 'General'