Python 读取excel表1单元格 生成 表2 的超链接

from openpyxl import load_workbook

# 加载现有的工作簿
from openpyxl.utils import get_column_letter

wb = load_workbook("C:\\Users\\CMS01\\Desktop\\SCHH621TEG_LDO(PLDO、NLDO、CPLDO)测试需求_20230814.xlsx")
# 获取Sheet1和Sheet6
sheet1 = wb['Sheet7']
sheet6 = wb['Sheet6']
cell_positions = []
cell1_positions = []

for column in range(2, 323, 4):
    column_letter = get_column_letter(column)  # 将列号转换为字母表示的列名
    cell_positions.append(f"{column_letter}5")
    cell1_positions.append(f"{column_letter}758")
    print(f"{column_letter}5" + "----" + f"{column_letter}758")  # B5 F5 J5 N5 ...


# 在Sheet1中创建超链接到Sheet6中指定单元格的值
column_index = 1  # 开始填充链接的列索引(从1开始)
for row_index, cell_position in enumerate(cell_positions, start=1):
    link_formula = f'=HYPERLINK("#\'Sheet6\'!{cell_position}","Sheet6!{cell_position}")'
    sheet1.cell(row=row_index, column=column_index).value = link_formula

column_index = 2  # 开始填充链接的列索引(从2开始)
for row_index, cell_position in enumerate(cell1_positions, start=1):
    link_formula = f'=HYPERLINK("#\'Sheet6\'!{cell_position}","Sheet6!{cell_position}")'
    sheet1.cell(row=row_index, column=column_index).value = link_formula


# 保存工作簿
wb.save("C:\\Users\\CMS01\\Desktop\\SCHH621TEG_LDO(PLDO、NLDO、CPLDO)测试需求_20230814.xlsx")

posted @ 2023-09-07 17:43  羽小兮  阅读(134)  评论(0)    收藏  举报