파이썬

데이터프레임을 엑셀파일로 저장시 시간이 지나면 엑셀파일을 열지 못하도록하는 파이썬 코드

파라만 2023. 2. 9. 09:57
728x90
반응형

보안을 위해 데이터프레임을 엑셀 파일로 저장하고, 지정된 시간이 지나면 파일을 열지 못하도록 하려면 다음과 같은 코드를 사용할 수 있습니다. 이 기능은 엑셀 파일에 대한 암호 보호 등 보안 기능을 통해 구현될 수 있습니다.

import pandas as pd
from datetime import datetime, timedelta

# Create a sample dataframe
df = pd.DataFrame({'col1': [1, 2, 3, 4], 'col2': [10, 20, 30, 40]})

# Set the expiration date for the file
expiration_date = datetime.now() + timedelta(days=7)

# Save the dataframe to an excel file with password protection
writer = pd.ExcelWriter('df.xlsx', engine='openpyxl')
df.to_excel(writer, index=False)
writer.save()

# Add password protection to the excel file
book = writer.book
sheet = writer.sheets['Sheet1']
sheet.protection.set_password('password')

# Add a custom property to the file with the expiration date
book.custom_properties = {'Expiration Date': expiration_date.strftime('%Y-%m-%d %H:%M:%S')}
book.save('df.xlsx')

엑셀 파일을 열려는 경우에는 기본 암호 보호 기능을 통해 파일의 암호를 확인하고, 그 외에도 애플리케이션에서 Expiration Date 프로퍼티를 조회하여 현재 시각과 비교하여 파일이 만료되었는지 확인할 수 있습니다.

728x90
반응형