본문 바로가기

파이썬

데이터프레임에서 엑셀로 변환시 사용기간 만기 기간을 정할 수 있는 코드

728x90
반응형

안타깝게도 팬더 데이터 프레임을 엑셀 파일로 변환하고 만료 날짜를 설정하는 작업은 팬더만 사용하여 수행할 수 없습니다. 그러나 openpyxl과 같은 타사 라이브러리를 사용하여 Excel 파일의 셀 만료 날짜를 설정할 수 있습니다.

다음은 예입니다:

import pandas as pd
from openpyxl import Workbook
from openpyxl.utils import get_column_letter

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# Create an Excel Workbook object
book = Workbook()

# Select the first sheet
writer = pd.ExcelWriter('example.xlsx', engine='openpyxl')
df.to_excel(writer, index=False, sheet_name='Sheet1')

# Select the sheet
sheet = writer.sheets['Sheet1']

# Set the expiration date for a cell
expiry_date = '2023-02-02'
cell = sheet.cell(row=2, column=3)
cell.value = expiry_date
cell.number_format = 'MM/DD/YYYY'

# Save the workbook
writer.save()

이 예에서는 만료 날짜를 2023-03-02로 설정하고 시트 1의 2열과 3열에 위치한 셀에서 MM/DD/YYY로 포맷합니다.

728x90
반응형