Python XlsxWriter – Text Box

Python XlsxWriter – Text Box

In Excel, a text box is a graphic object that can be placed anywhere in a worksheet and, if necessary, moved. Desired formatting features, such as font (color, size, name, etc.), alignment, fill effects, and orientation, can be applied to the text contained within the text box.

Using XlsxWriter – Text Box

In XlsxWriter, there is an insert_textbox() method for placing a text box in a worksheet. You must specify the cell location of the text box and the text to be written. Furthermore, various formatting options are given as a dictionary object.

Example

The following code displays a text box in cell C5, displaying the given string with the font and alignment properties shown below.

import xlsxwriter

wb = xlsxwriter.Workbook('hello.xlsx')
worksheet = wb.add_worksheet()
text = 'Welcome to TutorialsPoint'

options = {'font': {'color': 'red', 'size': 14},
'align': {'vertical': 'middle', 'horizontal': 'center'}}
worksheet.insert_textbox('C5', text, options)

wb.close()

Output

Open the worksheet “hello.xlsx” in the Excel application. The text box appears as follows –

Python XlsxWriter - Text Box

Text Box Options – Fill

The default size of a text box is 192×120 pixels (equivalent to 3 columns and 6 rows). This size can be changed using the width and height parameters, both of which are measured in pixels. One of the parameters accepted by the inset_textbox() method is the fill parameter. It takes a predefined color name or a hexadecimal representation of a color as its value.

Example

The following code displays a multi-line string in a custom-sized text box with a red background fill.

import xlsxwriter
wb = xlsxwriter.Workbook('hello.xlsx')
worksheet = wb.add_worksheet()
text = 'TutorialsPoint - Simple Easy LearningnThe best resource for Online Education'

options = {
'width': 384,
'height': 80,
'font': {'color': 'blue', 'bold': True, 'size': 14},
'align': {'vertical': 'middle', 'horizontal': 'center'},
'fill': {'color': 'red'},
}
worksheet.insert_textbox('C5', text, options)
wb.close()

As we can see in the image below, a multi-line text box is rendered in cell C5.

Python XlsxWriter - Text Box

Text Box Options – text_rotation

Another important property is text_rotation. By default, text appears horizontally. If desired, you can change its orientation by specifying an angle as its value. See the options below.

import xlsxwriter
wb = xlsxwriter.Workbook('hello.xlsx')
worksheet = wb.add_worksheet()
text = 'TutorialsPoint - Simple Easy LearningnThe best resource for Online 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 vertically in the text box.

Python XlsxWriter - Text Box

object_position parameter controls the behavior of the text box. It can have the following possible values and their effect: −

  • “1” – Moves and resizes with the cell (default).

  • “2” – Moves but does not resize with the cell.

  • “3” – Does not move or resize with the cell.

Leave a Reply

Your email address will not be published. Required fields are marked *