数字格式处理
设置单元格数字格式
将单元格的value设置为数字
设置单元格的number_format就可以更改excel中的显示格式
from openpyxl import Workbook
filename = 'number.xlsx'
wb = Workbook()
ws = wb.active
ws.column_dimensions['A'].width = 40
ws['A1'] = 111111
ws['A1'].number_format = '#,##0'
ws['A2'] = 111111.55
ws['A2'].number_format = '"¥"#,##0.00;-"¥"#,##0.00'
wb.save(filename)
输出结果如下图所示
下面列出部分格式设置对应表
格式化前的数字为1111111.50和-1111111.50
部分number_format中带有_)
,表示自动对齐标点
number_format | 正数显示格式 | 负数显示格式 |
---|---|---|
0 |
1111112 | -1111112 |
0.00 |
1111111.50 | -1111111.50 |
#,##0 |
1,111,112 | -1,111,112 |
#,##0.00 |
1,111,111.50 | -1,111,111.50 |
0.00E+00 |
1.11E+06 | -1.11E+06 |
# ?/? |
1111111 1/2 | -1111111 1/2 |
#,##0_);(#,##0) |
1,111,112 | -1,111,112 |
"¥"#,##0.00_);("¥"#,##0.00) |
¥1,111,111.50 | (¥1,111,111.50) |
"¥"#,##0.00;("¥"#,##0.00) |
¥1,111,111.50 | (¥1,111,111.50) |
"¥"#,##0.00;-"¥"#,##0.00 |
¥1,111,111.50 | -¥1,111,111.50 |
$#,##0.00;-$#,##0.00 |
$1,111,111.50 | -$1,111,111.50 |