
- Python XlsxWriter 教程
- Python XlsxWriter - 首頁
- Python XlsxWriter - 概述
- Python XlsxWriter - 環境搭建
- Python XlsxWriter - Hello World
- Python XlsxWriter - 重要類
- Python XlsxWriter - 單元格表示法和範圍
- Python XlsxWriter - 定義名稱
- Python XlsxWriter - 公式和函式
- Python XlsxWriter - 日期和時間
- Python XlsxWriter - 表格
- Python XlsxWriter - 應用篩選器
- Python XlsxWriter - 字型和顏色
- Python XlsxWriter - 數字格式
- Python XlsxWriter - 邊框
- Python XlsxWriter - 超連結
- Python XlsxWriter - 條件格式化
- Python XlsxWriter - 新增圖表
- Python XlsxWriter - 圖表格式化
- Python XlsxWriter - 圖表圖例
- Python XlsxWriter - 條形圖
- Python XlsxWriter - 折線圖
- Python XlsxWriter - 餅圖
- Python XlsxWriter - 迷你圖
- Python XlsxWriter - 資料驗證
- Python XlsxWriter - 大綱和分組
- Python XlsxWriter - 凍結和分割窗格
- Python XlsxWriter - 隱藏/保護工作表
- Python XlsxWriter - 文字框
- Python XlsxWriter - 插入圖片
- Python XlsxWriter - 頁面設定
- Python XlsxWriter - 頁首和頁尾
- Python XlsxWriter - 單元格批註
- Python XlsxWriter - 使用 Pandas
- Python XlsxWriter - VBA 宏
- Python XlsxWriter 有用資源
- Python XlsxWriter - 快速指南
- Python XlsxWriter - 有用資源
- Python XlsxWriter - 討論
Python XlsxWriter - 數字格式
在 Excel 中,數字資料的不同格式選項在“格式單元格”選單的“數字”選項卡中提供。

要使用 XlsxWriter 控制數字的格式,我們可以使用set_num_format()方法或定義add_format()方法的num_format屬性。
f1 = wb.add_format() f1.set_num_format(FormatCode) #or f1 = wb.add_format('num_format': FormatCode)
Excel 有許多預定義的數字格式。它們可以在數字選項卡的自定義類別下找到,如上圖所示。例如,帶有兩位小數和逗號分隔符的數字的格式程式碼是#,##0.00。
示例
在下面的示例中,數字 1234.52 使用不同的格式程式碼進行格式化。
import xlsxwriter wb = xlsxwriter.Workbook('hello.xlsx') ws = wb.add_worksheet() ws.set_column('B:B', 30) num=1234.52 num_formats = ( '0.00', '#,##0.00', '0.00E+00', '##0.0E+0', '₹#,##0.00', ) ws.write('A1', 'Formatted Number') ws.write('B1', 'Format') row = 1 for fmt in num_formats: format = wb.add_format({'num_format': fmt}) ws.write_number(row, 0, num, format) ws.write_string(row, 1, fmt) row += 1 wb.close()
輸出
格式化的數字及其使用的格式程式碼顯示在下圖中:

廣告