File: textbox.py

package info (click to toggle)
xlsxwriter 3.1.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 56,308 kB
  • sloc: python: 51,511; javascript: 7,768; sh: 284; makefile: 195; perl: 75
file content (205 lines) | stat: -rw-r--r-- 4,499 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#######################################################################
#
# An example of inserting textboxes into an Excel worksheet using
# Python and XlsxWriter.
#
# SPDX-License-Identifier: BSD-2-Clause
# Copyright 2013-2023, John McNamara, jmcnamara@cpan.org
#
import xlsxwriter

workbook = xlsxwriter.Workbook("textbox.xlsx")
worksheet = workbook.add_worksheet()
row = 4
col = 1

# The examples below show different textbox options and formatting. In each
# example the text describes the formatting.


# Example
text = "A simple textbox with some text"
worksheet.insert_textbox(row, col, text)
row += 10

# Example
text = "A textbox with changed dimensions"
options = {
    "width": 256,
    "height": 100,
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "A textbox with an offset in the cell"
options = {
    "x_offset": 10,
    "y_offset": 10,
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "A textbox with scaling"
options = {
    "x_scale": 1.5,
    "y_scale": 0.8,
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "A textbox with some long text that wraps around onto several lines"
worksheet.insert_textbox(row, col, text)
row += 10

# Example
text = "A textbox\nwith some\nnewlines\n\nand paragraphs"
worksheet.insert_textbox(row, col, text)
row += 10

# Example
text = "A textbox with a solid fill background"
options = {
    "fill": {"color": "red"},
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "A textbox with a no fill background"
options = {
    "fill": {"none": True},
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "A textbox with a gradient fill background"
options = {
    "gradient": {"colors": ["#DDEBCF", "#9CB86E", "#156B13"]},
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "A textbox with a user defined border line"
options = {
    "border": {"color": "red", "width": 3, "dash_type": "round_dot"},
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "A textbox with no border line"
options = {
    "border": {"none": True},
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "Default alignment: top - left"
worksheet.insert_textbox(row, col, text)
row += 10

# Example
text = "Alignment: top - center"
options = {
    "align": {"horizontal": "center"},
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "Alignment: middle - center"
options = {
    "align": {"vertical": "middle", "horizontal": "center"},
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "Alignment: long text line that wraps and is centered"
options = {
    "align": {"vertical": "middle", "horizontal": "center", "text": "center"},
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "Font properties: bold"
options = {
    "font": {"bold": True},
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "Font properties: various"
options = {
    "font": {"bold": True},
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "Font properties: various"
options = {
    "font": {
        "bold": True,
        "italic": True,
        "underline": True,
        "name": "Arial",
        "color": "red",
        "size": 12,
    }
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "Some text in a textbox with formatting"
options = {
    "font": {"color": "white"},
    "align": {"vertical": "middle", "horizontal": "center"},
    "gradient": {"colors": ["red", "blue"]},
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = ""
options = {
    "textlink": "=$F$185",
}
worksheet.write("F185", "Text in a cell")
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "Text rotated up"
options = {"text_rotation": 90}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "Text rotated down"
options = {"text_rotation": -90}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "Text rotated vertically"
options = {"text_rotation": 270}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = "Textbox with hyperlink"
options = {"url": "https://github.com/jmcnamara", "tip": "GitHub"}
worksheet.insert_textbox(row, col, text, options)
row += 10

workbook.close()