pandas handles Excel files just as easily as CSV files, using the openpyxl engine behind the scenes (installed once with pip).
Installing the Required Package (One-Time Setup)
R Code
# run this once in your terminal, not inside a script
pip install openpyxlReading an Excel File
R Code
import pandas as pd df = pd.read_excel("students.xlsx") print(df)
Output
name score 0 Rafi 88 1 Meera 92
Reading a Specific Sheet
R Code
# list every sheet name first xls = pd.ExcelFile("students.xlsx") print(xls.sheet_names) df = pd.read_excel("students.xlsx", sheet_name="Term2") df2 = pd.read_excel("students.xlsx", sheet_name=1) # or by position
Output
['Term1', 'Term2']
Writing an Excel File
R Code
df.to_excel("students_out.xlsx", index=False)
Advanced: Writing Multiple Sheets in One File
🚀 Advanced Example
ExcelWriter lets you write several DataFrames into the same Excel file, each as a separate named sheet.R Code
with pd.ExcelWriter("report.xlsx") as writer: df.to_excel(writer, sheet_name="Term1", index=False) df2.to_excel(writer, sheet_name="Term2", index=False)
Key Points
- pandas.read_excel() and .to_excel() need the openpyxl package installed (pip install openpyxl).
- Use ExcelFile(...).sheet_names to see what sheets exist before choosing one with sheet_name=.
- ExcelWriter lets you write multiple DataFrames as separate sheets in one output file.