How to Change Font, Color, and Size in Excel Using Python

Casie LiuCasie Liu
3 min read

When dealing with tables or hierarchical data, highlighting different levels of importance with distinct font styles can greatly enhance readability. Whether you're preparing a report or a financial statement, knowing how to change fonts, colors, and sizes in Excel workbooks is essential. In this tutorial, I'll walk you through automating font settings in Excel using Python, saving you time and boosting productivity.

How to Change Cell Font, Color, and Size in Excel Using Python

Changing fonts, colors, and sizes in Excel tables can significantly enhance readability, allowing readers to quickly focus on key information. In this chapter, we'll explore how to efficiently modify cell formatting using Python. Additionally, I'll demonstrate the process with the free Spire.XLS for Python, a user-friendly and powerful Excel component. Let's dive in!

Steps to change the font of a cell in Excel files:

  • Create an object of the Workbook class.

  • Read an Excel spreadsheet from files using the Workbook.LoadFromFile() method.

  • Get a worksheet through the Workbook.Worksheets.get_Item() method, and retrieve a cell with the Worksheet.Range.get_Item() method.

  • Set new font, color or size for the cell using properties under the CellRange.Style.Font class.

  • Save the updated Excel file through the Workbook.SaveToFile() method.

Here is a code example for changing the font of cell A3 on the third worksheet:

from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load a workbook from files
workbook.LoadFromFile("/text.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets.get_Item(2)

# Get a cell from the worksheet
cell = sheet.Range.get_Item(3, 1)

# Customize and change the font of the cell
cell.Style.Font.FontName = "Times New Roman"
cell.Style.Font.Color = Color.get_Blue()
cell.Style.Font.Size = 16
cell.Style.Font.IsBold = True
cell.Style.Font.IsItalic = True

# Save the Excel file
workbook.SaveToFile("/changefont-cell.xlsx")

Python Change Font of Cells in Excel

💡
There are some limitations of the free version. If you're working with large Excel files or require more advanced features, the commercial version may be more suitable for your needs.

How to Change the Entire Worksheet's Font in Excel with Python

If you want to modify the font across an entire Excel worksheet, you'll first need to access all the used cell ranges through the Worksheet.AllocatedRange property. Once you've got the ranges, you can easily adjust the font, size, color, and more. In this section, we'll walk through the steps and provide code examples to help you quickly apply font modifications to your worksheet.

Steps to change the font for the entire Excel worksheet:

  • Create an instance of the Workbook class.

  • Load a source Excel workbook through the Workbook.LoadFromFile() method.

  • Retrieve a worksheet with the Workbook.Worksheets.get_Item() method.

  • Get all the used ranges in this worksheet using the Worksheet.AllocatedRange property.

  • Set new font, color, or size for the cell range through properties under the CellRange.Style.Font class.

  • Save the modified Excel file.

The code example below demonstrates how to change the font style on the third worksheet:

from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load a workbook from files
workbook.LoadFromFile("/text.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets.get_Item(2)

# Get all used range
cell = sheet.AllocatedRange

# Change the font of the cell
cell.Style.Font.FontName = "Times New Roman"
cell.Style.Font.Color = Color.get_Blue()
cell.Style.Font.Size = 16
cell.Style.Font.IsBold = True
cell.Style.Font.IsItalic = True

# Save the Excel file
workbook.SaveToFile("/changefont-sheet.xlsx")

Change Font for The Entire Worksheet in Excel with Python

Conclusion

Today's guide walks you through changing the font, color, and size in Excel files, whether for individual cells or the entire worksheet. With clear, step-by-step instructions and code examples, this blog makes it easy for both beginners and experts to automate the process efficiently. No matter your skill level, you'll be able to apply these techniques seamlessly.

0
Subscribe to my newsletter

Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Casie Liu
Casie Liu