- Python XlsxWriter - VBA Macro
- Python XlsxWriter - Working with Pandas
- Python XlsxWriter - Cell Comments
- Python XlsxWriter - Header & Footer
- Python XlsxWriter - Page Setup
- Python XlsxWriter - Insert Image
- Python XlsxWriter - Textbox
- Python XlsxWriter - Hide/Protect Worksheet
- Python XlsxWriter - Freeze & Split Panes
- Python XlsxWriter - Outlines & Grouping
- Python XlsxWriter - Data Validation
- Python XlsxWriter - Sparklines
- Python XlsxWriter - Pie Chart
- Python XlsxWriter - Line Chart
- Python XlsxWriter - Bar Chart
- Python XlsxWriter - Chart Legends
- Python XlsxWriter - Chart Formatting
- Python XlsxWriter - Adding Charts
- Python XlsxWriter - Conditional Formatting
- Python XlsxWriter - Hyperlinks
- Python XlsxWriter - Border
- Python XlsxWriter - Number Formats
- Python XlsxWriter - Fonts & Colors
- Python XlsxWriter - Applying Filter
- Python XlsxWriter - Tables
- Python XlsxWriter - Date and Time
- Python XlsxWriter - Formula & Function
- Python XlsxWriter - Defined Names
- Python XlsxWriter - Cell Notation & Ranges
- Python XlsxWriter - Important classes
- Python XlsxWriter - Hello World
- Python XlsxWriter - Environment Setup
- Python XlsxWriter - Overview
- Python XlsxWriter - Home
Python XlsxWriter Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python XlsxWriter - Textbox
In Excel, a text box is a graphic object that can be placed anywhere on the worksheet, and can be moved around if needed. Desired formatting features such as font (color, size, name etc.), apgnment, fill effects, orientation etc. can be appped on the text contained in the text box.
Working with XlsxWriter – Textbox
In XlsxWriter, there is insert_textbox() method to place text box on the worksheet. The cell location of the text box and the text to be written in it must be given. Additionally, different formatting options are given in the form of a dictionary object.
Example
The following code displays a text box at cell C5, the given string is displayed with font and apgnment properties as shown below −
import xlsxwriter wb = xlsxwriter.Workbook( hello.xlsx ) worksheet = wb.add_worksheet() text = Welcome to TutorialsPoint options = { font : { color : red , size : 14}, apgn : { vertical : middle , horizontal : center }} worksheet.insert_textbox( C5 , text, options) wb.close()
Output
Open the worksheet hello.xlsx with Excel app. The text box appears as below −
Textbox Options – fill
The text box is by default 192X120 pixels in size (corresponds to 3 columns and 6 rows). This size can be changed with width and height parameters, both given in pixels. One of the parameters acceptable to inset_textbox() method is the fill parameter. It takes a predefined color name or color representation in hexadecimal as value.
Example
The following code displays a multi-pne string in the custom sized text box having background filled with red color.
import xlsxwriter wb = xlsxwriter.Workbook( hello.xlsx ) worksheet = wb.add_worksheet() text = TutorialsPoint - Simple Easy Learning The best resource for Onpne Education options = { width : 384, height :80, font : { color : blue , bold :True, size : 14}, apgn : { vertical : middle , horizontal : center }, fill :{ color : red }, } worksheet.insert_textbox( C5 , text, options) wb.close()
As we can see in the figure below, a text box with multiple pnes is rendered at cell C5.
Textbox Options – text_rotation
Another important property is the text_rotation. By default, the text appears horizontally. If required, you may change its orientation by giving an angle as its value. Look as the following options.
import xlsxwriter wb = xlsxwriter.Workbook( hello.xlsx ) worksheet = wb.add_worksheet() text = TutorialsPoint - Simple Easy Learning The best resource for Onpne Education options = { width : 128, height :200, font : { bold :True, name : Arial , size : 14}, text_rotation :90, } worksheet.insert_textbox( C5 , text, options) wb.close()
The text now appears in the text box with its vertical orientation.
The object_position parameter controls the behaviour of the text box. It can have the following possible values and their effect −
"1" − Move and size with cells (the default).
"2" − Move but don t size with cells.
"3" − Don t move or size with cells.