概述

worksheet接口主要是对工作表的处理,如获取内容、设置内容、清除内容、获取总行数、选中区域等

详情

get_cell

获取工作表中指定单元格的内容

get_cell(self, row_num, col_name)
参数:

  • row_num:指定单元格的行号,行号从1开始
  • col_name:指定单元格的列名,列名从’A’开始

返回值:

  • any:返回工作表中指定单元格内容

示例1:
获取工作表中第 10 行第 B 列单元格的内容

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. data = worksheet.get_cell(10, 'B')

get_row

获取工作表中指定行内容

get_row(self, row_num)
参数:

  • row_num:指定行号,行号从1开始

返回值:

  • List[any]:返回读取到的内容列表,如[‘a’, ‘b’, ‘c’, ‘d’]

示例1:
获取工作表中第一行内容

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. data = worksheet.get_row(1)

get_column

获取工作表中指定列内容

get_column(self, col_name)
参数:

  • col_name:指定列名,列名从’A’开始

返回值:

  • List[any]:返回读取到的内容列表,如[‘a’, ‘b’, ‘c’, ‘d’]

示例1:
获取工作表中第’A’列内容

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. data = worksheet.get_column('A')

get_range

获取工作表中指定区域的内容

get_range(self, begin_row_num, begin_column_name, end_row_num, end_column_name)
参数:

  • begin_row_num:起始单元格行号,行号从1开始
  • begin_column_name:起始单元格列名,列名从A开始
  • end_row_num:结束单元格行号, 行号从1开始
  • end_column_name:结束单元格列名,列名从A开始

返回值:

  • List[Tuple]:返回读取到的内容,二维结构如[ [‘a1’, ‘b1’], [‘a2’, ‘b2’] ]

示例1:
获取工作表中 起始单元格A1 到 结束单元格F3 的矩形区域内容

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. data = worksheet.get_range(1, 'A', 3, 'F')

set_cell

设置工作表指定单元格内容

set_cell(self, row_num, col_name, value)
参数:

  • row_num:指定单元格的行号
  • col_name:指定单元格的列名
  • value:要设置到单元格中的内容

返回值:

示例1:
设置工作表第 10 行第 B 列单元格的值为 ‘helloword’

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. worksheet.set_cell(10, 'B', 'helloword')

set_row

设置工作表行内容

set_row(self, row_num, values, begin_column_name = ‘A’)
参数:

  • row_num:要设置的行号,行号从1开始
  • values:要设置的值,必须是一个列表类型,如[‘a’, 1, 2]
  • begin_column_name:设置开始的单元格列名,默认值为 'A'

返回值:

示例1:
将列表数据 [‘a’, 1, 2] 写入工作表的第 1 行

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. worksheet.set_row(1, ['a', 1, 2], begin_column_name = 'A')

set_column

设置工作表列内容

set_column(selef, col_name, values, begin_row_num = 1)
参数:

  • col_name:需要设置的列名
  • values:要设置的值, 必须是一个列表类型
  • begin_row_num:需要设置的列的起始行号,行号从1开始

返回值:

示例1:
从第 1 行开始设置第 A 列数据值为 [1,2,3,4,5,6,7,8,9,0]

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. worksheet.set_column('A', [1,2,3,4,5,6,7,8,9,0], begin_row_num = 1)

set_range

设置工作表指定区域内容

set_range(self, row_num, col_name, values)
参数:

  • row_num:设置区域起始行号,行号从1开始
  • col_name:设置区域起始列名,列名从A开始
  • values:要设置的内容,必须是一个二维数组

返回值:

示例1:
设置工作表从 A1 单元格开始的值为

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. worksheet.set_range(1, 'A', [['1','2','3'],['a','b','c']])

append_row

在工作表的最后追加一行内容

append_row(self, values, begin_column_name = ‘A’)
参数:

  • values:要设置的值, 必须是一个列表类型
  • begin_column_name:设置开始的单元格列名, 默认值为A

返回值:

示例1:
在工作表的最后追加一行内容,起始写入单元格的列名为 B,内容为 [1,2,3,4,5,6,7,8,9,0]

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. worksheet.append_row([1,2,3,4,5,6,7,8,9,0], begin_column_name = 'B')

insert_row

在工作表中插入一行内容

insert_row(self, row_num, values, begin_column_name = ‘A’)
参数:

  • row_num:插入位置的行号,行号从1开始
  • values:要设置的值,必须是一个列表类型
  • begin_column_name:设置开始的单元格列名,默认值为A

返回值:

示例1:
往工作表第 10 行插入一条记录,行开始单元格列名为 A 插入值为 [1,2,3,4,5,6,7,8,9,0]

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. worksheet.insert_row(10, [1,2,3,4,5,6,7,8,9,0], begin_column_name = 'A')

remove_row

移除工作表的某一行内容

remove_row(self, row_num)
参数:

  • row_num:要移除的行号

返回值:

示例1:
移除工作表中第 10 行的数据

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. worksheet.remove_row(10)

remove_column

移除工作表的某一列内容

remove_column(self, column_name)
参数:

  • column_name:要移除的列名

返回值:

示例1:
移除工作表中第 D 列的数据

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. worksheet.remove_column('D')

clear

清空工作表内容

clear(self)
参数:

返回值:

示例1:
清空工作表内容

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. worksheet.clear()

get_row_count

获取工作表的总行数

get_row_count(self)
参数:

返回值:

  • int:返回工作表的总行数

示例1:
获取工作表当前数据的总行数

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. count = worksheet.get_row_count()

get_column_count

获取工作表的总列数

get_column_count(self)
参数:

返回值:

  • int:返回工作表的总列数

示例1:
获取工作表当前数据的总列数

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. count = worksheet.get_column_count()

select_range

选中工作表的指定内容区域

select_range(self, begin_row_num, begin_column_name, end_row_num, end_column_name)
参数:

  • begin_row_num:起始单元格行号,行号从1开始
  • begin_column_name:起始单元格列名,列名从A开始
  • end_row_num:结束单元格行号, 行号从1开始
  • end_column_name:结束单元格列名,列名从A开始

返回值:

示例1:
选中工作表中 从单元格A1到单元格C3 的矩形内容区域

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. count = worksheet.select_range(1, 'A', 3, 'C')

select_rows

选中工作表多行

select_rows(self, rows)
参数:

  • rows:行号列表,例如[1, 3]

返回值:

示例1:
选中工作表的第1、3、5行

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. worksheet.select_rows([1,3,5])

select_columns

选中工作表多列

select_columns(self, columns)
参数:

  • columns:列名列表,例如[‘A’, ‘D’]

返回值:

示例1:
选中工作表的第A、D列

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. worksheet.select_columns(['A', 'D'])

get_name

获取工作表的名称

get_name(self)
参数:

返回值:

  • str:返回工作表的名称

示例1:
获取工作表当前数据的名称

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. count = worksheet.get_name()

append_column

在工作表中追加列

append_column(self, values, begin_row_index = 1)
参数:

  • values:值列表
  • begin_row_index:待插入的起始单元格行号, 行号从1开始

返回值:

示例1:
在当前工作表中追加列

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. worksheet.append_column([123, 'abc'])

insert_column

插入列

select_range(self, column_name, values, begin_row_index = 1)
参数:

  • column_name:插入列名
  • values:插入数据
  • begin_row_index:开始插入的行号

返回值:

示例1:
将列表[1, ‘a’, 2]插入到D列,并且从第3行开始插

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. count = worksheet.insert('D', [1, 'a', 2], 3)

get_first_free_column

获取第一个可用列

get_first_free_column(self)
参数:

返回值:

  • str:返回工作表的列名

示例1:
获取当前列表的第一个可用列

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. count = worksheet.get_first_free_column()

get_first_free_row

获取第一个可用行

get_first_free_row(self)
参数:

返回值:

  • int:返回工作表的行号

示例1:
获取当前列表的第一个可用行

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. count = worksheet.get_first_free_row()

get_first_free_row_on_column

获取列的第一个可用列

get_first_free_row_on_column(self, column_name)
参数:

  • column_name:列名

返回值:

  • int:返回工作表的行号

示例1:
获取当前列表的D列上的第一个可用行

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. count = worksheet.get_first_free_row_on_column('D')

copy_range

拷贝指定的区域内容到剪切板

copy_range(self, begin_row_num, begin_column_name, end_row_num, end_column_name)
参数:

  • begin_row_num:起始单元格行号,行号从1开始
  • begin_column_name:起始单元格列名,列名从A开始
  • end_row_num:结束单元格行号, 行号从1开始
  • end_column_name:结束单元格列名,列名从A开始

返回值:

示例1:
拷贝工作表中从单元格A1到单元格C3 的矩形内容区域

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. count = worksheet.copy_range(1, 'A', 3, 'C')

paste_range

从指定的起始单元格粘贴剪切板数据

paste_range(self, row_num, column_name, copy_formula=True)
参数:

  • row_num:起始单元格行号,行号从1开始
  • column_name:起始单元格列名,列名从A开始
  • copy_formula:是否拷贝单元格公式

返回值:

示例1:
从指定的起始单元格C3粘贴剪切板数据

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. count = worksheet.paste_range(3, 'C')

copy_rows

拷贝工作表的多行

copy_rows(self, rows)
参数:

  • rows:行号列表,行号从1开始

返回值:

示例1:
拷贝当前工作表的[1,3,5]行

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. count = worksheet.copy_rows([1,3,5])

copy_columns

拷贝工作表的多列

copy_columns(self, columns)
参数:

  • columns:列名列表,列名从A开始

返回值:

示例1:
拷贝当前工作表的[‘A’, ‘D’]列

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. count = worksheet.copy_columns(['A', 'D'])

clear_range

清空指定的区域内容

clear_range(self, begin_row_num, begin_column_name, end_row_num, end_column_name)
参数:

  • begin_row_num:起始单元格行号,行号从1开始
  • begin_column_name:起始单元格列名,列名从A开始
  • end_row_num:结束单元格行号, 行号从1开始
  • end_column_name:结束单元格列名,列名从A开始

返回值:

示例1:
清空工作表中从单元格A1到单元格C3 的矩形内容区域

  1. from xbot import excel
  2. def main(args):
  3. workbook = excel.open('D:\\test.xlsx', kind = 'office', visible = True)
  4. worksheet = workbook.get_active_sheet()
  5. count = worksheet.clear_range(1, 'A', 3, 'C')