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, jmcnamara@cpan.org
#
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()

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值