Python for Excel users · Lesson 3 of 14
Open Excel and CSV files in Python
Read .xlsx workbooks and CSV files into pandas, choose sheets and columns, handle European number formats and save the result back to Excel.
In Excel you double-click a file. In pandas you call pd.read_excel() for workbooks and pd.read_csv() for CSV text files. Both give you a DataFrame, and both have options for the things you would fix by hand after opening.
Reading an Excel workbook
Reading and writing .xlsx files needs the small openpyxl library, installed once with pip install openpyxl in a terminal. The code below runs on your own computer with your own files, so it is shown but not run on this page.
import pandas as pd
# the first sheet
df = pd.read_excel("sales.xlsx")
# a sheet by name, only some columns, skip 2 title rows above the header
jan = pd.read_excel("sales.xlsx", sheet_name="January",
usecols="A:D", skiprows=2)
# every sheet at once: a dictionary {sheet name: DataFrame}
sheets = pd.read_excel("sales.xlsx", sheet_name=None)
for name, table in sheets.items():
print(name, len(table))usecols="A:D" takes Excel column letters; skiprows jumps over title rows above the headers.
Reading a CSV file
A CSV file is plain text: one line per row, commas between the cells. The examples on this page use a small sales.csv file that is created for you, so you can run them.
import pandas as pd
df = pd.read_csv("sales.csv")
print(df)
print(df.dtypes)Date Region Product Units Price 0 2026-03-02 North Lamp 4 19.99 1 2026-03-02 South Chair 2 45.00 2 2026-03-03 North Desk 1 120.00 3 2026-03-04 East Lamp 6 19.99 4 2026-03-05 South Desk 2 120.00 5 2026-03-05 East Chair 3 45.00 Date object Region object Product object Units int64 Price float64 dtype: object
dtypes shows what pandas understood each column to be: int64 whole numbers, float64 decimals, object text. The dates are still text here; lesson 11 turns them into real dates.
import pandas as pd
df = pd.read_csv("sales.csv", usecols=["Region", "Units"], nrows=3)
print(df)Region Units 0 North 4 1 South 2 2 North 1
usecols picks columns and nrows reads only the first rows, handy for a quick look at a big file. dtype={"Code": str} would keep leading zeros in codes such as 00123.
European CSV files: ; and decimal commas
Excel in many European countries saves CSV files with semicolons between cells and commas in numbers, like 1.250,50. Tell pandas with sep, decimal and thousands, otherwise the amounts arrive as text.
import pandas as pd
df = pd.read_csv("clients.csv", sep=";", decimal=",", thousands=".")
print(df)
print("Total:", df["Amount"].sum())Name City Amount 0 Ana Porto 1250.50 1 Ben Lyon 980.00 2 Chloe Turin 2100.75 Total: 4331.25
Saving your work
to_csv writes a CSV file. Pass index=False, otherwise the 0, 1, 2 row labels become an extra first column.
import pandas as pd
df = pd.read_csv("sales.csv")
north = df[df["Region"] == "North"]
north.to_csv("north.csv", index=False)
with open("north.csv", encoding="utf-8") as f:
print(f.read())Date,Region,Product,Units,Price 2026-03-02,North,Lamp,4,19.99 2026-03-03,North,Desk,1,120.0
Opening the new file again shows exactly what was written. The desk price became 120.0: a CSV keeps values, not number formats such as two decimals.
Writing to Excel works the same way with to_excel. To put several tables on separate sheets of one workbook, use pd.ExcelWriter. Close the workbook in Excel first: Windows locks open files, and pandas stops with a PermissionError.
import pandas as pd
df = pd.read_csv("sales.csv")
df.to_excel("sales_copy.xlsx", index=False, sheet_name="All")
with pd.ExcelWriter("report.xlsx") as writer:
for region, part in df.groupby("Region"):
part.to_excel(writer, sheet_name=region, index=False)One sheet per region, the job you would otherwise do with filter, copy and paste.
✎ Exercise
Read stock.csv (a European file with ; and decimal commas) and print the table, then the total of the Value column rounded to 2 decimals.
Hint: Use sep=";" and decimal="," in read_csv.
import pandas as pd
# your code here
Expected output:
Item Value 0 Paper 12.40 1 Toner 48.90 2 Pens 7.25 68.55
Show a solution
import pandas as pd
df = pd.read_csv("stock.csv", sep=";", decimal=",")
print(df)
print(round(df["Value"].sum(), 2))Item Value 0 Paper 12.40 1 Toner 48.90 2 Pens 7.25 68.55
Check yourself
1. Which argument stops to_csv from writing the 0, 1, 2 row labels as an extra column?
index=False leaves out the row labels; header=False would drop the column names.
2. How do you read every sheet of a workbook at once?
sheet_name=None returns a dictionary with one DataFrame per sheet.
3. A CSV file shows amounts like 1.250,50. What do you add to read_csv?
decimal says which character separates decimals, thousands which one groups thousands.