Python Excel操作模块XlsxWriter之添加文本框 worksheet.insert_textbox()

worksheet.insert_textbox()

insert_textbox(row, col, textbox[, options])

向工作表单元格添加文本框。

参数:

  • row(int) - 单元格所在的行(索引从0开始计数)。
  • col(int) - 单元格所在的列(索引从0开始计数)。
  • text(string) - 文本框里的文本。
  • options(dict) - 可选的文本框位置,缩放参数。

这个方法用于向工作表插入文本框:

worksheet.insert_textbox('B2', 'A simple textbox with some text')

文本框的大小和格式可以通过options字典操控:

# 大小和位置
width
height
x_scale
y_scale
x_offset
y_offset

# 格式
line
border
fill
gradient
font
align


注意

由于字体大于默认字体大小或打开了文本换行,则文本框的缩放可能会受到影响,因为它的默认高度已更改。如果它与插入的图表交叉,你应该使用set_row()显式的设置行高来避免此问题。


例:向工作表插入文本框

下面是一个如何向工作表插入和格式化文本框的例子。


#######################################################################
#
# An example of inserting textboxes into an Excel worksheet using
# Python and XlsxWriter.
#
# Copyright 2013-2017, John McNamara, [email protected]
#
import xlsxwriter

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

# 下面的例子展示了不同的文本框选项和格式。 


# 例子
text = 'A simple textbox with some text'
worksheet.insert_textbox(row, col, text)
row += 10

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

# 例子
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

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

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

# 例子
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

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

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

# 例子
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

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

# 例子
text = 'Default alignment: top - left'
worksheet.insert_textbox(row, col, text)
row += 10

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

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

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

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

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

# 例子
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

# 例子
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


workbook.close()

你可能感兴趣的:(Python)