Python XlsxWriter Tutorial
Python XlsxWriter Useful Resources
Selected Reading
- Python XlsxWriter - VBA Macro
- Python XlsxWriter - Working with Pandas
- Python XlsxWriter - Cell Comments
- Python XlsxWriter - Header & Footer
- Python XlsxWriter - Page Setup
- Python XlsxWriter - Insert Image
- Python XlsxWriter - Textbox
- Python XlsxWriter - Hide/Protect Worksheet
- Python XlsxWriter - Freeze & Split Panes
- Python XlsxWriter - Outlines & Grouping
- Python XlsxWriter - Data Validation
- Python XlsxWriter - Sparklines
- Python XlsxWriter - Pie Chart
- Python XlsxWriter - Line Chart
- Python XlsxWriter - Bar Chart
- Python XlsxWriter - Chart Legends
- Python XlsxWriter - Chart Formatting
- Python XlsxWriter - Adding Charts
- Python XlsxWriter - Conditional Formatting
- Python XlsxWriter - Hyperlinks
- Python XlsxWriter - Border
- Python XlsxWriter - Number Formats
- Python XlsxWriter - Fonts & Colors
- Python XlsxWriter - Applying Filter
- Python XlsxWriter - Tables
- Python XlsxWriter - Date and Time
- Python XlsxWriter - Formula & Function
- Python XlsxWriter - Defined Names
- Python XlsxWriter - Cell Notation & Ranges
- Python XlsxWriter - Important classes
- Python XlsxWriter - Hello World
- Python XlsxWriter - Environment Setup
- Python XlsxWriter - Overview
- Python XlsxWriter - Home
Python XlsxWriter Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python XlsxWriter - Number Formats
Python XlsxWriter - Number Formats
In Excel, different formatting options of numeric data are provided in the Number tab of Format Cells menu.
To control the formatting of numbers with XlsxWriter, we can use the set_num_format() method or define num_format property of add_format() method.
f1 = wb.add_format() f1.set_num_format(FormatCode) #or f1 = wb.add_format( num_format : FormatCode)
Excel has a number of predefined number formats. They can be found under the custom category of Number tab as shown in the above figure. For example, the format code for number with two decimal points and comma separator is #,##0.00.
Example
In the following example, a number 1234.52 is formatted with different format codes.
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()
Output
The formatted number along with the format code used is shown in the following figure −
Advertisements