Python: Set or Change Fonts in Excel

2023-08-25 07:05:38 Written by Koohji

Fonts play a crucial role in enhancing the visual appeal and readability of data in Microsoft Excel. Whether you're creating a spreadsheet, designing a report, or simply organizing information, the ability to set or change fonts can greatly impact the overall presentation. Excel offers a wide range of font options, allowing you to customize the style, size, and formatting to suit your specific needs. In this article, you will learn how to set or change fonts in Excel in Python using Spire.XLS for Python.

Install Spire.XLS for Python

This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.XLS

If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows

Set Different Fonts for Different Cells in Python

With Spire.XLS for Python, customizing fonts in specific cells becomes a breeze. By utilizing the CellRange.Style.Font property, you gain control over font name, color, size, and style effortlessly. Follow these steps to apply a font style to a particular cell using Spire.XLS for Python.

  • Create a Workbook object.
  • Get a specific worksheet through Workbook.Worksheets[index] property.
  • Get a specific cell through Worksheet.Range[int Row, int Column] property.
  • Set the value of the cell through CellRange.Value property.
  • Set the font name, color, size and style of the cell value through the properties under the CellRange.Style.Font object.
  • Save the workbook to an Excel file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object 
workbook = Workbook()

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Set font name
row = 1
sheet.Range[row, 1].Value = "Font Name"
sheet.Range[row, 2].Value = "Arial Black"
sheet.Range[row, 2].Style.Font.FontName = "Arial Black"

# Set font size
row += 2
sheet.Range[row, 1].Value = "Font Size"
sheet.Range[row, 2].Value = "15"
sheet.Range[row, 2].Style.Font.Size = 15

# Set font color 
row += 2
sheet.Range[row, 1].Value = "Font Color"
sheet.Range[row, 2].Value = "Red"
sheet.Range[row, 2].Style.Font.Color = Color.get_Red()

# Make text bold
row += 2
sheet.Range[row, 1].Value = "Bold"
sheet.Range[row, 2].Value = "Bold"
sheet.Range[row, 2].Style.Font.IsBold = True;

# Make text italic 
row += 2
sheet.Range[row, 1].Value = "Italic"
sheet.Range[row, 2].Value = "Italic"
sheet.Range[row, 2].Style.Font.IsItalic = True

# Underline text
row += 2
sheet.Range[row, 1].Value = "Underline"
sheet.Range[row, 2].Value = "Underline"
sheet.Range[row, 2].Style.Font.Underline = FontUnderlineType.Single

# Strikethrough text 
row += 2
sheet.Range[row, 1].Value = "Strikethrough "
sheet.Range[row, 2].Value = "Strikethrough "
sheet.Range[row, 2].Style.Font.IsStrikethrough = True

# Set column width
sheet.Columns[0].ColumnWidth = 25
sheet.Columns[1].ColumnWidth = 25

# Save the workbook to an Excel file
workbook.SaveToFile("output/ApplyFontInCell.xlsx", ExcelVersion.Version2016)

Python: Set or Change Fonts in Excel

Apply Multiple Fonts in a Single Cell in Python

To emphasize specific characters within a cell, you can mix fonts. Here are the steps to apply multiple fonts in a single cell using Spire.XLS for Python:

  • Create a Workbook object.
  • Get a specific worksheet through Workbook.Worksheets[index] property.
  • Create two ExcelFont objects using Workbook.CreateFont() method.
  • Get a specific cell through Worksheet.Range[int Row, int Column] property, and set the rich text content of the cell through CellRange.RichText.Text property.
  • Apply the two ExcelFont objects to the rich text using RichText.SetFont() method.
  • Save the workbook to an Excel file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Create a font
font1 = workbook.CreateFont()
font1.FontName = "Arial Black"
font1.KnownColor = ExcelColors.LightBlue
font1.IsBold = True
font1.Size = 13

# Create another font
font2 = workbook.CreateFont()
font2.KnownColor = ExcelColors.Red
font2.IsBold = True
font2.IsItalic = True
font2.FontName = "Algerian"
font2.Size = 15;

# Returns a RichText object from a specified cell
richText = sheet.Range["A1"].RichText

# Set the text of RichText object
richText.Text = "Buy One, Get One Free"

# Apply the first font to specified range of characters
richText.SetFont(0, 16, font1)

# Apply the second font to specified range of characters
richText.SetFont(17, 21, font2)

# Set column width
sheet.Columns[0].ColumnWidth = 33

# Save the workbook to an Excel file
workbook.SaveToFile("output/ApplyMultipleFontsInSingleCell.xlsx", ExcelVersion.Version2016)

Python: Set or Change Fonts in Excel

Change the Font Style of a Cell Range in Python

Spire.XLS for Python offers the CellStyle class, enabling users to handle cell formatting like fill color, text alignment, and font style. By creating a cell style, you can apply it to a specific range of cells using the CellRange.ApplyStyle() method or to an entire worksheet using the Worksheet.ApplyStyle() method. To change the font style of a cell range using Spire.XLS for Python, follow these steps.

  • Create a Workbook object.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[index] property.
  • Create a CellStyle object using Workbook.Styles.Add() method, and set the font style through the CellStyle.Font property.
  • Apply the cell style to a cell range using CellRange.ApplyStyle() method.
  • Save the workbook to another Excel file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load a sample Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Create a CellStyle object
fontStyle = workbook.Styles.Add("headerFontStyle")

# Set the font color, size and style
fontStyle.Font.Color = Color.get_White()
fontStyle.Font.IsBold = True
fontStyle.Font.Size = 12

# Create a CellStyleFlag object, setting the FontColor, FontBold, ad FontSize properties to true
flag = CellStyleFlag()
flag.FontColor = True
flag.FontBold = True
flag.FontSize = True

# Apply the cell style to header row 
sheet.Range[1, 1, 1, 8].ApplyStyle(fontStyle, flag)

# Apply the cell style to the whole worksheet
# sheet.ApplyStyle(fontStyle)

# Save the workbook to another Excel file
workbook.SaveToFile("output/ApplyFontToCellRange.xlsx", ExcelVersion.Version2016)

Python: Set or Change Fonts in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Python: Convert Excel to Images

2023-08-24 00:54:56 Written by Koohji

Converting Excel spreadsheets to image formats can be extremely valuable and versatile in a wide range of situations. Whether you need to share data with others who don’t have Excel installed on their devices, present information in a document or presentation, or publish content online, converting Excel to image format offers a convenient solution. In this article, we will introduce how to programmatically convert Excel to images in Python using Spire.XLS for Python.

Install Spire.XLS for Python

This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.XLS

If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows

Convert an Excel Worksheet to an Image in Python

You can easily convert a whole Excel worksheet to an image by using the Worksheet.SaveToImage() method provided by Spire.XLS for Python. The detailed steps are as follows:

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet by its index using Workbook.Worksheets[int index] property.
  • Convert the worksheet to an image using Worksheet.ToImage() method.
  • Save the image to a PNG file (you can also save the image as other image formats such as JPG and BMP).
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Save the worksheet to an image
image = sheet.ToImage(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn)
# Save the image to a PNG file
image.Save("SheetToImage.png")
workbook.Dispose()

Python: Convert Excel to Images

Convert an Excel Worksheet to an Image without White Margins in Python

When converting an Excel worksheet to an image, you may find the resulting image has unwanted white margins surrounding the cells. If you want to convert the worksheet to an image without any extraneous margins, you can remove the page margins set in the original worksheet. The detailed steps are as follows:

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet by its index using Workbook.Worksheets[int index] property.
  • Remove all margins from the worksheet by setting its left, right, top, and bottom margin values to zero.
  • Convert the worksheet to an image using Worksheet.ToImage() method.
  • Save the image to a PNG file.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Set all margins of the worksheet to zero
sheet.PageSetup.LeftMargin = 0
sheet.PageSetup.BottomMargin = 0
sheet.PageSetup.TopMargin = 0
sheet.PageSetup.RightMargin = 0

# Convert the worksheet to an image
image = sheet.ToImage(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn)
# Save the image to a PNG file
image.Save("SheetToImageWithoutMargins.png")
workbook.Dispose()

Python: Convert Excel to Images

Convert a Specific Cell Range to an Image in Python

In addition to converting a whole worksheet to an image, Spire.XLS for Python also supports converting a specific cell range of a worksheet to an image. The detailed steps are as follows:

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet by its index using Workbook.Worksheets[int index] property.
  • Convert a specific cell range of the worksheet to an image using Worksheet.ToImage() method and pass the index of the start row, start column, end row, and end column of the cell range to the method as parameters.
  • Save the image to a PNG file.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Convert a specific cell range of the worksheet to an image
image = sheet.ToImage(5, 2, 17, 5)
# Save the image to a PNG file
image.Save("CellRangeToImage.png")
workbook.Dispose()

Python: Convert Excel to Images

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Inserting or deleting images in Excel can be a powerful way to enhance your spreadsheets and make them visually appealing. Whether you want to include logos, charts, diagrams, or any other graphical elements, Excel provides the functionality to seamlessly integrate images into your worksheets. Additionally, Excel offers the options to manipulate and organize images, allowing you to resize, move, or delete them as needed. In this article, we will explore how to programmatically insert or delete pictures in Excel in Python by using Spire.XLS for Python.

Install Spire.XLS for Python

This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.XLS

If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows

Insert a Picture to a Specific Cell in Python

To add a picture to a certain cell, you use Worksheet.Pictures.Add(int topRow, int leftColumn, Image image) method. The following are the detailed steps.

  • Initialize a Workbook instance.
  • Get a specific worksheet through Workbook.Worksheets[sheetIndex] property.
  • Insert a picture into a specific cell using Worksheet.Pictures.Add() method and return an object of ExcelPicture.
  • Set the width and height of the picture, as well as the distance between the picture and the cell border through the properties under the ExcelPicture object.
  • Save the workbook to an Excel file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *
     
# Create a Workbook object
workbook = Workbook()

# Get the first worksheet.
sheet = workbook.Worksheets[0]

# Add a picture to a specific cell
imgPath = "C:\\Users\\Administrator\\Desktop\\logo.png"
picture = sheet.Pictures.Add(1, 3, imgPath)

# Set the picture width and height
picture.Width = 150
picture.Height = 150

# Adjust the column width and row height so that the cell can accommodate the picture
sheet.Columns[2].ColumnWidth = 25
sheet.Rows[0].RowHeight = 135

# Set the distance between cell border and image
picture.LeftColumnOffset = 90
picture.TopRowOffset = 20

# Save to file
workbook.SaveToFile("output/InsertImage.xlsx", ExcelVersion.Version2013)
workbook.Dispose()

Python: Insert or Delete Pictures in Excel

Delete Pictures in a Worksheet in Python

A picture in a worksheet can be removed using Worksheet.Pictures[imgIndex].Remove() method. To delete all pictures, you can use a for loop to iterate through the pictures in the worksheet. The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet through Workbook.Worksheets[sheetIndex] property.
  • Delete images in the worksheet using Worksheet.Pictures[imgIndex].Remove() method.
  • Save the workbook to another Excel file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load an Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\InsertImage.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Delete all pictures from the worksheet
for i in range(sheet.Pictures.Count - 1, -1, -1):
    sheet.Pictures[i].Remove()

# Delete a specific picture
# sheet.Pictures[imgIndex].Remove()

# Save to file
workbook.SaveToFile("output/DeleteImage.xlsx", ExcelVersion.Version2013)
workbook.Dispose()

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

page 76

Coupon Code Copied!

Christmas Sale

Celebrate the season with exclusive savings

Save 10% Sitewide

Use Code:

View Campaign Details