티스토리 뷰
다수 개의 시트가 존재하는 엑셀파일의 경우,
각 시트를 이동하는 과정은 1) 시트탭을 하나씩 살펴가면서 좌우로 이동해야 하고 2) 시트명이 유사한 경우 잘못 누를 수 있고 3) 특정 시트를 찾기 위해 전체 시트를 여러 번 훑어야 하는 낭비가 발생할 수 있음.
Python읜 openpyxl을 이용하여 데이터를 관리하는 경우에도 유사한 문제가 발생할 수 있음.
이를 위해 'Overview' 시트를 만들고 이 시트 내 데이터에 나머지 시트의 개괄적인 정보와 함께 이 시트로 직접 이동할 수 있도록 하는 링크를 저장하고자 함.
* 엑셀 함수로는 '=HYPERLINK("Sheet1!A1", "Sheet1-Link")'
[코드]
def Test_HyperLink():
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
# 엑셀 워크북 생성
wb = Workbook()
# 기본 시트 이름 변경
a_sheet = wb.active
a_sheet.title = "Index"
# 생성할 시트 이름 리스트
sheet_names = ["Sheet1", "Sheet2", "Sheet3"]
# 시트 생성
def create_sheets(wb, sheet_names):
for name in sheet_names:
wb.create_sheet(title=name)
# 하이퍼링크 추가 함수
def add_hyperlinks(sheet, sheet_names):
sheet.append(["Sheet Name", "Link"])
for idx, name in enumerate(sheet_names, start=2):
link = f"'#'{name}'!A1" # 시트로 이동하는 하이퍼링크
sheet[f"A{idx}"] = name
sheet[f"B{idx}"].value = f"Go to {name}"
sheet[f"B{idx}"].hyperlink = link
sheet[f"B{idx}"].style = "Hyperlink"
# 시트 생성 및 하이퍼링크 설정
create_sheets(wb, sheet_names)
add_hyperlinks(a_sheet, sheet_names)
# 엑셀 파일 저장
wb.save("excel_with_links.xlsx")
print("엑셀 파일이 생성되었습니다: excel_with_links.xlsx")
반응형
'SWDesk' 카테고리의 다른 글
[Python] Transform between datetime and timestamp (0) | 2025.04.06 |
---|---|
[Python] FFT, sort, subplot (0) | 2025.02.08 |
[Python] 숫자를 문자로 변환할 때 앞에 '0' 붙이는 코드 (0) | 2024.12.10 |
2024년 소프트웨어 개발 환경 (2) | 2024.09.04 |
주요 알고리즘 (0) | 2024.08.28 |
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 치매방지
- Video
- ServantClock
- Innovations
- 둎
- 아두이노
- Decorator
- 전압
- Innovations&Hurdles
- 빌리언트
- 심심풀이치매방지기
- 심심풀이
- 혁신과허들
- 전압전류모니터링
- 절연형
- 전류
- Innovation&Hurdles
- bilient
- 배프
- DYOV
- 치매
- Hurdles
- badp
- 티스토리챌린지
- 허들
- image
- BSC
- arduino
- 혁신
- 오블완
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함