xlwt的Worksheet.remove_name函数介绍
Worksheet.remove_name函数是xlwt中的一个方法,用于删除Worksheet中的特定名称
使用方法
remove_name
方法有一个必需的参数,就是要删除的名称(Name)。而且,该名称必须先在 Workbook 和以下工作表中进行了定义:Worksheet 和 Chart 子类:
remove_name(name)
例子1
假设我们已经在Workbook中添加了一些命名单元格,如下所示:
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet("mysheet")
worksheet.write(0, 0, "name")
worksheet.write(0, 1, "age")
named_style = xlwt.easyxf("pattern: pattern solid, fore-colour yellow; font: bold on")
worksheet.write(1, 0, "Tom", named_style) #命名单元格以名称`TOM`保存
worksheet.write(1, 1, 18)
name = "TOM"
name_index = workbook._all_names_map[name][0]
name_records = workbook.name_records
if name_records is not None and name_index < len(name_records):
name_record = name_records[name_index][_all_name_indices["BuiltInName"]:]
worksheet.remove_name(name)
workbook.save("test.xls")
例子2
假设我们使用Workbook和Worksheet进行如下定义:
import xlwt
workbook = xlwt.Workbook()
worksheet1 = workbook.add_sheet('sheet1')
worksheet2 = workbook.add_sheet('sheet2')
named_style = xlwt.easyxf("pattern: pattern solid, fore-colour yellow; font: bold on")
cell_value = 'Hello'
worksheet1.write(0, 0, cell_value, named_style)
worksheet2.write(0, 0, cell_value)
name_index = workbook._all_names_map[cell_name][0]
name_records = workbook.name_records
if name_records is not None and name_index < len(name_records):
name_record = name_records[name_index][_all_name_indices["BuiltInName"]:]
worksheet1.remove_name(name, building=False)
worksheet2.remove_name(name, building=False)
总结
Worksheet.remove_name方法是删除Worksheet中特定名称的函数。它有一个必需的参数,就是要删除的名称。即使名称已经在Workbook中定义,也必须使用方法,这就是为什么要先定义名称,然后才能删除它。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解xlwt的 Worksheet.remove_name 函数:删除命名区域 - Python技术站