[엑셀과 함께 춤을] Work 탭에서는 어떨까 ?

지금까지는 Web 탭에서 수행했던 내용인데, Work 탭의 동작은 어떨까요 ?

image


아무 말도 하지 않고 그냥 파일을 던져주니, 모든 시트의 내용을 다 읽고 분석, 정리한 내용을 보여줍니다.

Web 과 동일합니다.

image


코드를 보니 그럴만도 한 것이 그냥 통째로 파일을 읽었어요.

image


이번에는 앞서 했던 작업을 시켜봅니다. 하기는 하는데…. 시트 이름도 Sheet1 이라니? 표로 왜 안보여주니 ?

느낌이 쎄하죠?

image


코드를 보면 ‘한글 프롬프트’ 컬럼을 찾으려는 노력을 하는 것이 보입니다.

컬럼 아닌데….

import pandas as pd

# Load the Excel file
excel_file = pd.ExcelFile("뭔가통합 4.xlsx", engine="openpyxl")

# Dictionary to store prompts from each sheet
prompts_by_sheet = {}

# Iterate through each sheet and extract '한글 프롬프트' column
for sheet_name in excel_file.sheet_names:
    df = excel_file.parse(sheet_name)
    if '한글 프롬프트' in df.columns:
        prompts_by_sheet[sheet_name] = df['한글 프롬프트'].dropna().tolist()

# Display the collected prompts in a table format
for sheet, prompts in prompts_by_sheet.items():
    print(f"\n📄 Sheet: {sheet}")
    for i, prompt in enumerate(prompts, 1):
        print(f"{i}. {prompt}")

시트의 목록을 보여주는 건 동일하게 잘 합니다.

image


코드도 역시 기대했던 것과 다르지 않습니다.

import pandas as pd

# Load the Excel file and list all sheet names
excel_file = pd.ExcelFile("뭔가통합 4.xlsx", engine="openpyxl")
sheet_names = excel_file.sheet_names

# Display the sheet names
sheet_names

동일합니다. 시트의 열 목록이라..

image


시트의 행 목록을 보여달라고 하니 전부 잘 보여줍니다.

역시나 열이 세로축이고 행이 가로축이군요.

image

import pandas as pd

# Load the Excel file and list all sheet names
excel_file = pd.ExcelFile("뭔가통합 4.xlsx", engine="openpyxl")
sheet_name = excel_file.sheet_names[2]  # Third sheet

# Load the third sheet into a DataFrame
df = pd.read_excel(excel_file, sheet_name=sheet_name, engine="openpyxl")

# Convert all rows to a list of lists
rows = df.values.tolist()

# Display the rows
for row in rows:
    print(row)