How to Format, Copy, and Clear Excel Cell Formatting with Python
When working with Excel spreadsheets, cell content is only part of the picture. Fonts, colors, alignment, borders, and number formats all affect how data is presented and read. If you need to generate or process Excel files in batches, adjusting these settings manually can be time-consuming and make it harder to maintain a consistent style.
In this tutorial, we’ll use Spire.XLS for Python to format Excel cells, copy existing formatting to other cells or ranges, and clear formatting without removing the underlying data.
Prerequisites
Spire.XLS for Python is a Python library for creating, reading, editing, and saving Excel documents without requiring Microsoft Excel to be installed. In addition to working with worksheet data, it supports cell formatting, formulas, charts, data validation, and other Excel features, making it useful for automating spreadsheet tasks.
Install the library with pip:
pip install Spire.XLS
Then import the required modules:
from spire.xls import *
from spire.xls.common import *Let’s look at how to format, copy, and clear Excel cell formatting with practical examples.
Format Excel Cells with Python
Excel offers a range of formatting options, including fonts, alignment, borders, and background colors. With the Style property in Spire.XLS for Python, you can customize these settings to suit your spreadsheet.
The following examples cover common formatting tasks. A complete script that combines them is included at the end of this section.
1. Set the Font
Font settings are among the most frequently adjusted cell properties. Using Style.Font, you can change the font name, size, color, and weight.
For example, the following code sets the font in cell B2 to 14-point bold Microsoft YaHei in blue:
cell = sheet.Range[2, 2]
cell.Text = "Python Excel Formatting Example"
cell.Style.Font.FontName = "Microsoft YaHei"
cell.Style.Font.Size = 14
cell.Style.Font.Color = Color.get_Blue()
cell.Style.Font.IsBold = TrueHere, FontName specifies the font, Size sets the font size, Color determines the text color, and IsBold controls whether the text is bold.
2. Set Cell Alignment
Alignment affects how cell content appears, especially when preparing headers or working with long text. Spire.XLS lets you configure horizontal and vertical alignment separately, as well as control text wrapping with WrapText.
The following example centers the text horizontally and vertically and enables text wrapping:
cell = sheet.Range[4, 2]
cell.Text = "This text is centered and wraps within the cell."
cell.Style.HorizontalAlignment = HorizontalAlignType.Center
cell.Style.VerticalAlignment = VerticalAlignType.Center
cell.Style.WrapText = TrueWith text wrapping enabled, long content can appear on multiple lines within a cell instead of extending beyond its visible area.
3. Add Cell Borders
Borders help distinguish rows and columns and make data easier to scan. Spire.XLS lets you set the line style and color for individual cell borders.
For example, you can add a thin black border to the bottom of cell B6:
cell = sheet.Range[6, 2]
cell.Text = "Border Example"
cell.Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin
cell.Style.Borders[BordersLineType.EdgeBottom].Color = Color.get_Black()EdgeBottom specifies the bottom border, while LineStyleType.Thin sets its line style to thin.
4. Set the Cell Background Color
Background colors can help highlight headers, important values, or specific statuses. You can change a cell’s background using the Style.Color property.
For example, the following code gives cell B8 a yellow background:
cell = sheet.Range[8, 2]
cell.Text = "Highlighted Cell"
cell.Style.Color = Color.get_Yellow()You can also combine background colors with font and border settings to make important cells stand out.
5. Set Row Height and Column Width
In addition to cell styles, you can adjust row heights and column widths to improve the layout of a worksheet.
Get Carol Liu’s stories in your inbox
Join Medium for free to get updates from this writer.
For example, the following code sets the width of the first column to 20 and the height of the seventh row to 20:
# Set the width of the first column
sheet.Columns[1].ColumnWidth = 20
# Set the height of the seventh row
sheet.Rows[7].RowHeight = 20Increasing the width or height can make long text easier to read, especially when combined with text wrapping.
Complete Example: Apply Multiple Formatting Options
The following script creates a workbook, adds sample text to several cells, and applies different formatting options, including fonts, alignment, borders, background colors, row heights, and column widths.
from spire.xls import *
from spire.xls.common import *
# Create a workbook and get the first worksheet
workbook = Workbook()
sheet = workbook.Worksheets[0]
# Set the font
cell = sheet.Range[2, 2]
cell.Text = "Python Excel Formatting Example"
cell.Style.Font.FontName = "Microsoft YaHei"
cell.Style.Font.Size = 14
cell.Style.Font.Color = Color.get_Blue()
cell.Style.Font.IsBold = True
# Set alignment and enable text wrapping
cell = sheet.Range[4, 2]
cell.Text = "This text is centered and wraps within the cell."
cell.Style.HorizontalAlignment = HorizontalAlignType.Center
cell.Style.VerticalAlignment = VerticalAlignType.Center
cell.Style.WrapText = True
# Add a bottom border
cell = sheet.Range[6, 2]
cell.Text = "Border Example"
cell.Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin
cell.Style.Borders[BordersLineType.EdgeBottom].Color = Color.get_Black()
# Set the background color
cell = sheet.Range[8, 2]
cell.Text = "Highlighted Cell"
cell.Style.Color = Color.get_Yellow()
# Set column width
sheet.Columns[1].ColumnWidth = 20
# Set row height
sheet.Rows[7].RowHeight = 20
# Save the workbook
workbook.SaveToFile("SetCellFormatting.xlsx", ExcelVersion.Version2016)
workbook.Dispose()Copy Excel Cell Formatting with Python
Once you’ve formatted a cell, you don’t need to configure the same font, color, borders, and other properties again for every cell that needs the same style.
Spire.XLS lets you retrieve a cell’s existing Style and assign it to another cell or a range of cells. This makes it easier to reuse formatting and maintain a consistent appearance across a worksheet.
In the previous section, for example, cell B2 was formatted with a blue, bold, 14-point font. You can reuse that style in another cell. Similarly, you can apply the alignment and text-wrapping style from B4 to a larger range.
1. Copy Formatting from One Cell to Another
To copy formatting between two cells, retrieve the Style property from the source cell and assign it to the destination cell.
The following example copies the formatting from B2 to C2:
style = worksheet.Range[2, 2].Style
worksheet.Range[2, 3].Style = styleHere, B2 is the source cell, and C2 is the destination. The example assumes that B2 has already been formatted in the previous section.
Note: Copying the style this way does not overwrite the destination cell’s existing value or formula.
2. Apply One Cell’s Formatting to a Range
You can also apply a single cell’s style to multiple cells at once. For example, the following code applies the formatting from B4 to the range C4:D5:
style = worksheet.Range[4, 2].Style
worksheet.Range[4, 3, 5, 4].Style = styleThis is useful when several cells need the same appearance. Instead of changing each cell’s formatting properties individually, you can reuse an existing style.
Complete Example: Copy Formatting Between Cells and Ranges
The following script loads the workbook created in the previous section, adds sample text to the destination cells, copies the formatting from B2 to C2, and applies the formatting from B4 to C4. It then saves the result as a new file.
from spire.xls import *
from spire.xls.common import *
# Load the workbook created in the previous example
workbook = Workbook()
workbook.LoadFromFile("SetCellFormatting.xlsx")
# Get the first worksheet
worksheet = workbook.Worksheets[0]
# Add sample text to the destination cells
worksheet.Range[2, 3].Text = "Copied Formatting"
worksheet.Range[4, 3].Text = "Range Style 1"
worksheet.Range[4, 4].Text = "Range Style 2"
worksheet.Range[5, 3].Text = "Range Style 3"
worksheet.Range[5, 4].Text = "Range Style 4"
# Copy the formatting from B2 to C2
style = worksheet.Range[2, 2].Style
worksheet.Range[2, 3].Style = style
# Apply the formatting from B4 to C4:D5
style = worksheet.Range[4, 2].Style
worksheet.Range[4, 3, 5, 4].Style = style
# Save the result
workbook.SaveToFile("CopyCellFormatting.xlsx", ExcelVersion.Version2016)
workbook.Dispose()Clear Excel Cell Formatting with Python
Sometimes, you may want to remove cell formatting without deleting the underlying data. For example, you might need to clear unwanted colors, borders, or font settings while keeping the existing values and formulas.
Spire.XLS provides the Clear() method for this purpose. By passing ExcelClearOptions.ClearFormat, you can clear formatting from a selected cell or range without clearing its contents.
The following example loads the workbook generated in the previous section and clears formatting from a single cell and a larger range:
from spire.xls import *
from spire.xls.common import *
# Create a workbook and load the file
workbook = Workbook()
workbook.LoadFromFile("CopyCellFormatting.xlsx")
# Get the first worksheet
worksheet = workbook.Worksheets[0]
# Clear formatting from a single cell
worksheet.Range["B2"].Clear(ExcelClearOptions.ClearFormat)
# Clear formatting from a range of cells
worksheet.Range["B4:D7"].Clear(ExcelClearOptions.ClearFormat)
# Save the result
workbook.SaveToFile("ClearCellFormatting.xlsx", ExcelVersion.Version2016)
workbook.Dispose()The example clears formatting from B2 and B4. You can change these ranges to match the cells you want to reset.
ExcelClearOptions.ClearFormat removes cell formatting without deleting the cell values or formulas. If you need to clear formatting from an entire row or column, select the corresponding range and apply the same method. Be sure to choose the range carefully so that you don’t remove formatting that is still needed.
Conclusion
With Spire.XLS for Python, you can format Excel cells, reuse existing styles, and clear unwanted formatting programmatically. Use the Style property to configure fonts, alignment, borders, and background colors; assign an existing Style to copy formatting to other cells or ranges; and use Clear(ExcelClearOptions.ClearFormat) to remove formatting while preserving cell contents.
These techniques can help reduce repetitive manual work and make Excel formatting easier to automate, especially when processing multiple worksheets or generating workbooks programmatically.






