# excel表格 在某个单元格，右键，定义名称 怎么用python代码实现呢

2018-04-24 09:58:11

...全文
471 4 打赏 收藏 举报

4 条回复

l_num = ws.max_column # 最大列 h_num = ws.max_row # 最大行 print(ws.max_row) print(ws.max_column) a_list = [] for i in range(1, h_num + 1): # h_num + 1 # i= 2 for j in range(1, l_num + 1): p = ws.cell(row=1, column=j) pz = p.value # 第一行的值 字段 a = ws.cell(row=i, column=j) # 每列标题 # aa = a.value # 此单元格里边的值 写入新表格 coord = a.coordinate # 此单元格坐标 A1 B1 l = re.findall(r'^[A-Z]*', coord)[0] h = re.findall(r'[0-9]*\$', coord)[0] # 把sheet1 里的A2 设定为rpt_date # dname = DefinedName(name='rpt_date') # dname.value = "Sheet1!\$A\$2" bb = "Sheet1!\$" + l + '\$' + h dname = DefinedName(name='%s' % pz) dname.value = bb print('>>>', dname, '---', type(dname)) wb.defined_names.append(dname) wb.defi wb.save(xl_savefile) wb.close()
• 打赏
• 举报

``````
from openpyxl.workbook.defined_name import DefinedName
from openpyxl.utils import absolute_coordinate, quote_sheetname
xl_file = "d:/test.xlsx"
xl_savefile = "d:/test_save.xlsx"
if __name__ == '__main__':
ws = wb.active

# 把sheet1 里的A2 设定为rpt_date
dname = DefinedName(name='rpt_date')
dname.value = "Sheet1!\$A\$2"
wb.defined_names.append(dname)

wb.save(xl_savefile)
wb.close()
``````
• 打赏
• 举报

• 打赏
• 举报

• 打赏
• 举报

3.7w+

JavaScript，VBScript，AngleScript，ActionScript，Shell，Perl，Ruby，Lua，Tcl，Scala，MaxScript 等脚本语言交流。

2018-04-24 09:58