1####################################################################### 2# 3# An example of inserting textboxes into an Excel worksheet using 4# Python and XlsxWriter. 5# 6# Copyright 2013-2021, John McNamara, jmcnamara@cpan.org 7# 8import xlsxwriter 9 10workbook = xlsxwriter.Workbook('textbox.xlsx') 11worksheet = workbook.add_worksheet() 12row = 4 13col = 1 14 15# The examples below show different textbox options and formatting. In each 16# example the text describes the formatting. 17 18 19# Example 20text = 'A simple textbox with some text' 21worksheet.insert_textbox(row, col, text) 22row += 10 23 24# Example 25text = 'A textbox with changed dimensions' 26options = { 27 'width': 256, 28 'height': 100, 29} 30worksheet.insert_textbox(row, col, text, options) 31row += 10 32 33# Example 34text = 'A textbox with an offset in the cell' 35options = { 36 'x_offset': 10, 37 'y_offset': 10, 38} 39worksheet.insert_textbox(row, col, text, options) 40row += 10 41 42# Example 43text = 'A textbox with scaling' 44options = { 45 'x_scale': 1.5, 46 'y_scale': 0.8, 47} 48worksheet.insert_textbox(row, col, text, options) 49row += 10 50 51# Example 52text = 'A textbox with some long text that wraps around onto several lines' 53worksheet.insert_textbox(row, col, text) 54row += 10 55 56# Example 57text = 'A textbox\nwith some\nnewlines\n\nand paragraphs' 58worksheet.insert_textbox(row, col, text) 59row += 10 60 61# Example 62text = 'A textbox with a solid fill background' 63options = { 64 'fill': {'color': 'red'}, 65} 66worksheet.insert_textbox(row, col, text, options) 67row += 10 68 69# Example 70text = 'A textbox with a no fill background' 71options = { 72 'fill': {'none': True}, 73} 74worksheet.insert_textbox(row, col, text, options) 75row += 10 76 77# Example 78text = 'A textbox with a gradient fill background' 79options = { 80 'gradient': {'colors': ['#DDEBCF', 81 '#9CB86E', 82 '#156B13']}, 83} 84worksheet.insert_textbox(row, col, text, options) 85row += 10 86 87# Example 88text = 'A textbox with a user defined border line' 89options = { 90 'border': {'color': 'red', 91 'width': 3, 92 'dash_type': 'round_dot'}, 93} 94worksheet.insert_textbox(row, col, text, options) 95row += 10 96 97# Example 98text = 'A textbox with no border line' 99options = { 100 'border': {'none': True}, 101} 102worksheet.insert_textbox(row, col, text, options) 103row += 10 104 105# Example 106text = 'Default alignment: top - left' 107worksheet.insert_textbox(row, col, text) 108row += 10 109 110# Example 111text = 'Alignment: top - center' 112options = { 113 'align': {'horizontal': 'center'}, 114} 115worksheet.insert_textbox(row, col, text, options) 116row += 10 117 118# Example 119text = 'Alignment: middle - center' 120options = { 121 'align': {'vertical': 'middle', 122 'horizontal': 'center'}, 123} 124worksheet.insert_textbox(row, col, text, options) 125row += 10 126 127# Example 128text = 'Alignment: long text line that wraps and is centered' 129options = { 130 'align': {'vertical': 'middle', 131 'horizontal': 'center', 132 'text': 'center'}, 133} 134worksheet.insert_textbox(row, col, text, options) 135row += 10 136 137# Example 138text = 'Font properties: bold' 139options = { 140 'font': {'bold': True}, 141} 142worksheet.insert_textbox(row, col, text, options) 143row += 10 144 145# Example 146text = 'Font properties: various' 147options = { 148 'font': {'bold': True}, 149} 150worksheet.insert_textbox(row, col, text, options) 151row += 10 152 153# Example 154text = 'Font properties: various' 155options = { 156 'font': {'bold': True, 157 'italic': True, 158 'underline': True, 159 'name': 'Arial', 160 'color': 'red', 161 'size': 12} 162} 163worksheet.insert_textbox(row, col, text, options) 164row += 10 165 166# Example 167text = 'Some text in a textbox with formatting' 168options = { 169 'font': {'color': 'white'}, 170 'align': {'vertical': 'middle', 171 'horizontal': 'center' 172 }, 173 'gradient': {'colors': ['red', 'blue']}, 174} 175worksheet.insert_textbox(row, col, text, options) 176row += 10 177 178# Example 179text = '' 180options = { 181 'textlink': '=$F$185', 182} 183worksheet.write('F185', 'Text in a cell') 184worksheet.insert_textbox(row, col, text, options) 185row += 10 186 187# Example 188text = 'Text rotated up' 189options = { 190 'text_rotation': 90 191} 192worksheet.insert_textbox(row, col, text, options) 193row += 10 194 195# Example 196text = 'Text rotated down' 197options = { 198 'text_rotation': -90 199} 200worksheet.insert_textbox(row, col, text, options) 201row += 10 202 203# Example 204text = 'Text rotated vertically' 205options = { 206 'text_rotation': 270 207} 208worksheet.insert_textbox(row, col, text, options) 209row += 10 210 211# Example 212text = 'Textbox with hyperlink' 213options = { 214 'url': 'https://github.com/jmcnamara', 215 'tip': 'GitHub' 216} 217worksheet.insert_textbox(row, col, text, options) 218row += 10 219 220workbook.close() 221