Back to Python
2026-03-236 min read

Excel Format Numbers (Python Programming)

Learn Excel Format Numbers (Python Programming) step by step with clear examples and exercises.

Title: Excel Format Numbers (Python Programming)

Why This Matters

Excel format numbers are essential when working with data from an Excel spreadsheet in Python. This skill is crucial for data analysis, business intelligence, and automation tasks that involve handling large datasets stored in Excel files. Demonstrating proficiency in reading and writing Excel format numbers can showcase your expertise in Python and data manipulation during interviews.

Prerequisites

To follow this lesson, you should have a basic understanding of:

  1. Python programming concepts such as variables, functions, loops, and conditional statements
  2. Data structures like lists and dictionaries
  3. Reading and writing files using Python (e.g., with the built-in open() function)
  4. Basic familiarity with Excel file structure and common number formats used in Excel
  5. Understanding of object-oriented programming concepts (for working effectively with the openpyxl library)
  6. Familiarity with error handling, data type conversion, and exception handling in Python

Core Concept

Excel format numbers are represented as strings in Python. Each number has a specific format that includes information about the number's precision, thousands separator, decimal separator, and negative sign. The most common Excel formats are:

  1. General: 123456 (no specific format)
  2. Number: 123,456 (comma as a thousands separator)
  3. Scientific: 1.23E+05 (scientific notation)
  4. Percentage: 0.123456% or 12.3456% (percentage with or without percent sign)
  5. Date: 1/1/2022 (date format YYYY-MM-DD is not supported directly)
  6. Time: 1:00:00 AM (time format HH:MM:SS AM/PM)
  7. Text: "Text" (any non-numeric string)
  8. Boolean: TRUE or FALSE (representing True and False in Python)
  9. Error: #N/A, #NAME?, #NULL!, #DIV/0!, etc. (representing Excel errors)

Python provides the openpyxl library for reading and writing Excel files, which can handle these formats natively.

Understanding Openpyxl Library

Openpyxl is a Python library that allows you to read, write, and modify Excel 2010 (xlsx) and Excel 97-2003 (xls) files. It provides an object-oriented interface for working with spreadsheets and supports various data types found in Excel.

Key Openpyxl Concepts

  1. Workbook: Represents the entire Excel file.
  2. Sheet: Represents a single worksheet within the workbook.
  3. Cell: Represents an individual cell within a sheet.
  4. DataTypes: Supports various data types like integers, floats, strings, dates, and booleans.
  5. Styles: Allows you to manipulate cell styles, fonts, colors, and formatting options.

Worked Example

Let's create a Python script that reads an Excel file containing numbers in various formats and writes them to the console using the openpyxl library.

import openpyxl
from openpyxl.styles import NumberFormat

Load the workbook from the given file path

wb = openpyxl.load_workbook('numbers.xlsx')

Select the active sheet (usually Sheet1)

sheet = wb.active

Iterate through each row in the worksheet

for row in range(1, sheet.max_row + 1):

Iterate through each cell in the row

for col in range(1, sheet.max_column + 1):

Get the value of the current cell as a string

cell_value = str(sheet[f'A{row}'].value)

Print the cell value and its type

print(f"Cell A{row}, Column {col}: {cell_value}, Type: {type(cell_value)}")

Check if the cell contains a number format error

if '#' in cell_value:

print(f"Error detected in Cell A{row}, Column {col}: {cell_value}")

Check if the cell contains a date

elif re.match(r'\d{4}-\d{2}-\d{2}', cell_value):

Convert the date to datetime object

date = datetime.strptime(cell_value, '%Y-%m-%d')

print(f"Date detected in Cell A{row}, Column {col}: {date}")

Check if the cell contains a percentage

elif re.match(r'^\d+\.\d+%$', cell_value):

Convert the percentage to decimal form

percentage = float(cell_value.replace('%', '')) / 100

print(f"Percentage detected in Cell A{row}, Column {col}: {percentage}")

Common Mistakes

  1. Forgetting to install the openpyxl library before running the script.
  2. Assuming that Excel date format YYYY-MM-DD is supported directly, when it needs to be converted to a datetime object first.
  3. Not handling errors like #N/A, #NAME?, and others appropriately in your code.
  4. Using the wrong data type (e.g., integer instead of float) for Excel numbers that have decimal points.
  5. Neglecting to install the pandas library when dealing with large datasets, which can handle Excel files more efficiently than working directly with the openpyxl library.
  6. Not properly handling exceptions or errors during the execution of the script.
  7. Failing to use the correct regular expressions (regex) for pattern matching in cells containing dates and percentages.

Error Handling

When reading Excel files, it's essential to handle errors like #N/A, #NAME?, and others appropriately in your code. You can use conditional statements or exception handling techniques to check for these errors and handle them accordingly, such as skipping the cell or assigning a default value.

Data Type Conversion

When working with Excel numbers that have decimal points, ensure you use the correct data type (e.g., float) to avoid issues during calculations. Additionally, if you encounter Excel dates in format YYYY-MM-DD, convert them into a datetime object using the datetime library or pandas before performing any calculations.

Practice Questions

  1. Write a Python script that reads an Excel file containing numbers in various formats and calculates their sum using the openpyxl library.
  2. Given an Excel file with dates in format YYYY-MM-DD, write a Python function to convert these dates into datetime objects using the datetime library or pandas.
  3. Write a Python script that reads an Excel file containing errors like #N/A, #NAME?, and others, and prints the count of each error type using exception handling techniques.
  4. Given an Excel file with percentages in format X.XX%, write a Python function to convert these percentages to decimal form (e.g., 12.3456% to 0.123456) using regular expressions.
  5. Write a Python script that reads an Excel file containing numbers, converts them into floats, and sorts the data in ascending order using the openpyxl library and Python's built-in sorting functions.
  6. Given an Excel file with text data, write a Python function to count the number of unique words in each column using the collections library.
  7. Write a Python script that reads an Excel file containing numbers, calculates their average, and writes the result back to the same Excel file using the openpyxl library.
  8. Given an Excel file with dates in format YYYY-MM-DD, write a Python function to find the maximum date in each column using the datetime library or pandas.
  9. Write a Python script that reads an Excel file containing numbers and applies conditional formatting based on specific rules (e.g., highlighting cells with values greater than 100) using the openpyxl library's styles module.

FAQ

Q: Why can't I read Excel date format YYYY-MM-DD directly in Python?

A: Python doesn't support this format natively, but you can convert it into a datetime object using the datetime library or pandas.

Q: How do I handle errors like #N/A, #NAME?, and others when reading Excel files in Python?

A: You can use conditional statements to check for these errors and handle them appropriately, such as skipping the cell or assigning a default value. Alternatively, you can use exception handling techniques to catch these errors and handle them accordingly.

Q: What's the best way to work with large datasets stored in Excel files using Python?

A: Using the pandas library is recommended for handling large datasets efficiently. It provides functions for reading and writing Excel files, as well as data manipulation and analysis capabilities. If you still encounter performance issues, consider using a database management system like SQLite or PostgreSQL to store your data.

Q: How can I apply conditional formatting based on specific rules in an Excel file using Python?

A: You can use the openpyxl library's styles module to apply conditional formatting based on specific rules. This involves defining a ConditionalFormat object, setting its conditions (e.g., greater than 100), and applying it to the desired cells.

Q: How do I find the maximum date in each column of an Excel file using Python?

A: You can use the datetime library or pandas to convert dates into datetime objects, sort them, and find the maximum value for each column. Alternatively, you can use the openpyxl library's functions to iterate through cells in a column and find the maximum date directly.

Excel Format Numbers (Python Programming) | Python | XQA Learn