컴퓨터/Python

python 시트보호해제해서 다른이름으로 파일저장

풍경소리^^ 2024. 3. 15. 14:17

xls

# 시트보호해제하는파일
import xlrd
from datetime import datetime
from xlutils.copy import copy

# 현재 날짜를 가져와서 파일명에 사용
today_date = datetime.now().strftime("%Y%m%d")

# 엑셀 파일 경로
excel_file_path = "일별출퇴근기록표.xls"

# 새로운 파일명 설정
new_excel_file = f"일별출퇴근기록표{today_date}.xls"

# 엑셀 파일 열기 (읽기 전용)
workbook = xlrd.open_workbook(excel_file_path, formatting_info=True)

# 첫 번째 시트 선택
worksheet = workbook.sheet_by_index(0)

# 수정된 데이터를 기록하기 위해 복사본 생성
new_workbook = copy(workbook)
# new_worksheet = new_workbook.get_sheet(0)

# 각 셀을 순회하면서 #NUM! 값을 공백으로 변경
# for row_idx in range(worksheet.nrows):
#     for col_idx in range(worksheet.ncols):
#         cell_value = worksheet.cell_value(row_idx, col_idx)
#         if isinstance(cell_value, float) and cell_value == xlrd.XL_CELL_NUMBER and worksheet.cell_xf_index(row_idx, col_idx) == 2:
#             new_worksheet.write(row_idx, col_idx, "")

# 변경된 내용을 저장
new_workbook.save(new_excel_file)

print("엑셀 파일 수정이 완료되었습니다.")

 

xlsx

from openpyxl import Workbook
 
# 새로운 워크북 생성
workbook = Workbook()
sheet = workbook.active
 
# # 시트 보호 설정
# sheet.protection.sheet = True
 
# # 워크북 저장
# workbook.save("시트보호해제.xlsx")
 
# 시트 보호 해제
sheet.protection.sheet = False
 
# 워크북 저장
workbook.save("시트보호해제.xlsx")

https://workauto.tistory.com/entry/%ED%8C%8C%EC%9D%B4%EC%8D%AC-openpyxl-activeprotectionsheet-%ED%95%A8%EC%88%98-%ED%99%9C%EC%9A%A9%ED%95%98%EA%B8%B0