Textboxes in Excel provide a flexible way to add textual information or annotations to worksheets, charts, or other objects. They allow users to display explanatory text, labels, or comments that are not directly related to the data itself. In this guide, we will explore how to add, update, and delete textboxes in Excel in Python using Spire.XLS for Python.
- Add a Textbox to Excel in Python
- Update a Textbox in Excel in Python
- Delete a Textbox in Excel in 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
Add a Textbox to Excel in Python
A textbox can be added to the specified location of a worksheet using Worksheet.TextBoxes.AddTextBox() method. The TextBox object has a set of properties that allow you to set the text and formatting of the textbox. The detailed steps to create a textbox using Spire.XLS for Python are as follows.
- Create a Workbook object.
- Get a specific worksheet through Workbook.Worksheets[index] property.
- Add a textbox to the worksheet at the specified location using Worksheet.TextBoxes.AddTextBox() method.
- Set text of the textbox through TextBox.Text property.
- Set formatting of the text through other properties under the TextBox 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 textbox to the worksheet, specifying location and size textBox = sheet.TextBoxes.AddTextBox(5, 3, 120, 300) # Set fill color of the textbox textBox.Fill.FillType = ShapeFillType.SolidColor textBox.Fill.ForeKnownColor = ExcelColors.Gray25Percent # Add text to the textbox and set the text alignment textBox.Text = "This is a textbox in Excel." textBox.HAlignment = CommentHAlignType.Center textBox.VAlignment = CommentVAlignType.Center # Set font for the text font = workbook.CreateFont() font.FontName = "Times New Roman" font.Size = 18 font.IsBold = True font.Color = Color.get_Blue() richText = textBox.RichText rt = RichText(richText) rt.SetFont(0, len(textBox.Text) - 1, font) # Save the workbook to an Excel file workbook.SaveToFile('output/InsertTextbox.xlsx', ExcelVersion.Version2016) workbook.Dispose()
Update a Textbox in Excel in Python
A certain textbox can be accessed through Worksheet.TextBoxes[index] property and the text inside the box can be obtained or modified through TextBox.Text property. The following are the steps to update a textbox using Spire.XLS for Python.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets[index] property.
- Add a textbox to the worksheet at the specified location using Worksheet.TextBoxes.AddTextBox() method.
- Reset text of the textbox through TextBox.Text property.
- Save the workbook to a different Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.common import * # Create a Workbook object workbook = Workbook() # Load an Excel file workbook.LoadFromFile('C:\\Users\\Administrator\\Desktop\\Textbox.xlsx') # Get the first worksheet sheet = workbook.Worksheets[0] # Get the first textbox tb = sheet.TextBoxes[0] # Change the text of textbox tb.Text = "The text in this textbox was changed." # Save the workbook to a different Excel file workbook.SaveToFile('output/UpdateTextbox.xlsx', ExcelVersion.Version2016) workbook.Dispose()
Delete a Textbox in Excel in Python
To remove a specific textbox, you use Worksheet.TextBox[index].Remove() method. The detailed steps are as follows.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific sheet through Workbook.Worksheets[index] property.
- Remove a specific textbox by using Worksheet.TextBoxes[index].Remove() method.
- Save the workbook to a different Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.common import * # Create a Workbook object workbook = Workbook() # Load an Excel file workbook.LoadFromFile('C:\\Users\\Administrator\\Desktop\\Textbox.xlsx') # Get the first worksheet sheet = workbook.Worksheets[0] # Remove the first textbox sheet.TextBoxes[0].Remove() # Save the workbook to a different Excel file workbook.SaveToFile('output/RemoveTextbox.xlsx', ExcelVersion.Version2016) 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.