Python: Apply Superscript and Subscript in Excel

Superscript and subscript are formatting styles used in typography and writing to position characters or numbers above or below the normal line of text. Superscript is a smaller-sized text or symbol that is raised above the baseline. It is commonly used for mathematical exponents, footnotes, and ordinal indicators. Subscript, on the other hand, is a smaller-sized text or symbol that is positioned below the baseline. It is often used for chemical formulas, mathematical expressions and some linguistic notations. These formatting styles can help users distinguish specific elements within text and convey information more effectively. In this article, we will show you how to apply superscript and subscript in Excel 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

Apply Superscript and Subscript in Excel

To apply the superscript or subscript style to specific characters in excel, you need to create a custom font first and set the superscript or subscript property of it. And then assign the font to the specific characters within the cell using CellRange.RichText.SetFont() method provided by Spire.XLS for Python. The detailed steps are as follows:

  • Create an object of Workbook class.
  • Get the first worksheet of it using Workbook.Worksheets[int index] property.
  • Get the specific cells using Worksheet.Range[string name] property and add desired text to them.
  • Get a cell by using Worksheet.Range[string name] property and add rich text to it by CellRange.RichText.Text property.
  • Create a custom font using Workbook.CreateFont() method.
  • Enable the subscript property of the font by setting ExcelFont.IsSubscript property to true.
  • Assign the font to specific characters of the added rich text in the cell by calling CellRange.RichText.SetFont() method.
  • Likewise, get another cell using Worksheet.Range[string name] property and add rich text to it by CellRange.RichText.Text property.
  • Create a custom font using Workbook.CreateFont() method.
  • Enable the superscript property of the font by setting ExcelFont.IsSuperscript property to true.
  • Assign the font to specific characters of the added rich text in the cell by calling CellRange.RichText.SetFont() method.
  • Automatically adjust column widths to fit text length using Worksheet.AllocatedRange.AutoFitColumns() method.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
from spire.common import *
from spire.xls.common import *

outputFile = "ApplySubscriptAndSuperscript.xlsx"

#Create an object of Workbook class
workbook = Workbook()

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

#Add text to the specific cells
sheet.Range["B2"].Text = "This is an example of Subscript:"
sheet.Range["D2"].Text = "This is an example of Superscript:"

#Add rich text to a specific cell
range = sheet.Range["B3"]
range.RichText.Text = "an = Sn - Sn-1"

#Create a custom font
font = workbook.CreateFont()

#Enable the subscript property of the font by setting the IsSubscript property to "true"
font.IsSubscript = True

#Set the font color
font.Color = Color.get_Green()

#Assign the font to specific characters of the added rich text 
range.RichText.SetFont(6, 6, font)
range.RichText.SetFont(11, 13, font)

#Add rich text to another cell
range = sheet.Range["D3"]
range.RichText.Text = "a2 + b2 = c2"

#Create a custom font
font = workbook.CreateFont()

#Enable the superscript property of the font by setting the IsSuperscript property to "true"
font.IsSuperscript = True

#Assign the font to specific characters of the added rich text
range.RichText.SetFont(1, 1, font)
range.RichText.SetFont(6, 6, font)
range.RichText.SetFont(11, 11, font)

#Autofit the column widths
sheet.AllocatedRange.AutoFitColumns()
 
#Save the result file
workbook.SaveToFile(outputFile, ExcelVersion.Version2013)
workbook.Dispose()

Python: Apply Superscript and Subscript 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.