Python Xlsxwriter 简明教程
Python XlsxWriter - Textbox
在 Excel 中, text box 是一个图形对象,可以放置在工作表的任何位置,并且在需要时可以移动。可以将诸如字体(颜色、大小、名称等)、对齐方式、填充效果、方向等所需的格式化功能应用于文本框中包含的文本。
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.), alignment, fill effects, orientation etc. can be applied on the text contained in the text box.
Working with XlsxWriter – Textbox
在 XlsxWriter 中,一个 insert_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
以下代码在单元格 C5 中显示一个文本框,给定字符串将显示如下所示的字体和对齐方式属性:
The following code displays a text box at cell C5, the given string is displayed with font and alignment 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},
'align': {'vertical': 'middle','horizontal': 'center'}}
worksheet.insert_textbox('C5', text, options)
wb.close()
Output
使用 Excel 应用程序打开工作表“ hello.xlsx ”。文本框如下所示:
Open the worksheet 'hello.xlsx' with Excel app. The text box appears as below −

Textbox Options – fill
默认情况下,文本框大小为 192X120 像素(对应 3 列和 6 行)。此大小可以通过 width 和 height 参数进行更改,这两个参数都以像素为单位。 inset_textbox() 方法接受的参数之一是 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-line 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\nThe 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()
正如我们在下图中看到的,在单元格 C5 处呈现一个带有多行的文本框。
As we can see in the figure below, a text box with multiple lines is rendered at cell C5.

Textbox Options – text_rotation
另一个重要属性是 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\nThe 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 in the text box with its vertical orientation.

object_position 参数控制文本框的 behaviour 。它可以具有以下可能的值及其效果:
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.