相关疑难解决方法(0)

Openpyxl设置数字格式

可以请某人展示将数字格式应用于单元格的示例.例如,我需要科学的格式,形式就像'2.45E + 05',但我无法想象如何在openpyxl中做到这一点.

我试过几种方法但是在保存工作簿时它们都报告错误.

例如:

    import openpyxl as oxl

    wb = oxl.Workbook()
    ws = wb.create_sheet(title='testSheet')
    _cell = ws.cell('A1')
    _cell.style.number_format = '0.00E+00'
Run Code Online (Sandbox Code Playgroud)

或者这(我在这里尝试使用一些预定义的数字格式,我也看到内置工程格式,但不知道如何访问它:

    nf = oxl.style.NumberFormat.FORMAT_NUMBER_00
    _cell.style.number_format = nf
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,我都得到同样的错误:

C:\Python27\openpyxl\cell.pyc in is_date(self)
    408         """
    409         return (self.has_style
--> 410                 and self.style.number_format.is_date_format()
    411                 and isinstance(self._value, NUMERIC_TYPES))

AttributeError: 'str' object has no attribute 'is_date_format'
Run Code Online (Sandbox Code Playgroud)

我已经看到了这个问题:在Openpyxl中设置样式 但它没有帮助,因为我不必更改任何其他格式设置.

python openpyxl

26
推荐指数
3
解决办法
3万
查看次数

标签 统计

openpyxl ×1

python ×1