You read an Excel file in Python inside VS Code by installing a library (usually pandas with openpyxl), opening the folder that contains both your script and your spreadsheet, and then calling the read function with the correct filename and sheet name. The shortest working version is pip install pandas openpyxl in the VS Code terminal, followed by import pandas as pd and df = pd.read_excel("data.xlsx", sheet_name="Sheet1"). Everything below explains how to get that running reliably, plus what to do when the file will not load.

What You'll Need

  • Python installed and working. Confirm with python --version in a terminal.
  • VS Code, with the official Microsoft Python extension installed from the Extensions view.
  • An Excel file to test with (.xlsx or .xls).
  • Permission to install Python packages, since the libraries are not part of the standard library.
  • Basic terminal comfort: opening a panel, running one command, reading the output.

One clarification up front: VS Code is only the editor. It does not read Excel files itself. Python does the reading, and the extension gives you a terminal, a run button and a debugger to work with that Python.

Step-by-Step

  1. Create a project folder. Make a folder such as excel-demo and put your spreadsheet inside it. In VS Code choose File > Open Folder and select that folder. Opening the folder (not just the file) matters, because your terminal and file paths resolve from there.
  2. Create and select a virtual environment. Open the terminal with View > Terminal and run python -m venv .venv. Then select the interpreter: open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on macOS) and run Python: Select Interpreter, choosing the one inside .venv. On Windows activate with .venv\Scripts\activate; on macOS or Linux use source .venv/bin/activate. A virtual environment keeps these packages from colliding with other projects later.
  3. Install the libraries. In that terminal run python -m pip install pandas openpyxl. pandas is the table library; openpyxl is the engine that actually parses modern .xlsx files. If your workbook is the older .xls format, also install xlrd. Confirm with python -c "import pandas, openpyxl; print(pandas.__version__)".
  4. Write the script. Create read_excel.py in the same folder and start with something small and explicit. Import pandas, pass the exact filename, and print the result. Then run it with the Run button or python read_excel.py. Seeing the first five rows printed is your proof everything is wired correctly.
  5. Point at the right sheet. A workbook can hold many sheets. By default read_excel returns the first one. Pass sheet_name="Budget" to choose another, sheet_name=None to load every sheet at once as a dictionary of DataFrames, or sheet_name=1 for the second sheet by position. Print pd.ExcelFile("data.xlsx").sheet_names first if you are unsure what the sheets are called.
  6. Deal with headers and empty rows. If the first row is not your column names, use header=None so pandas does not treat it as labels. If there is a title row or blank rows above the table, use skiprows=2 to skip them. Reading with no options and eyeballing df.head() is the fastest way to spot header trouble.
  7. Inspect before you trust it. Run print(df.shape) for row and column counts, df.columns for names, and df.dtypes for inferred types. Excel stores everything as loosely typed cells, so a column of numbers with one stray text entry may arrive as text. Check df.isna().sum() for missing values before doing any maths.
  8. Convert values where needed. Use pd.to_numeric(df["Amount"], errors="coerce") to force text-stored numbers into real numbers, and pd.to_datetime(df["Date"]) for date columns. errors="coerce" turns anything unparseable into a missing value instead of crashing, which is usually what you want while cleaning.
  9. Save and reuse. Once the frame looks right, write it back or export it with df.to_csv("output.csv", index=False). If you use ef.to_excel, you need openpyxl installed as well, and index=False stops pandas from adding a row-number column that most people do not want.

Common Mistakes

  • Wrong working directory. The most frequent error is a file path that is correct in your file explorer but wrong for the terminal's current folder. Use the folder VS Code has open, or build an absolute path.
  • The file is open in Excel on Windows. That can lock the file and raise a permission error. Close the workbook in Excel first.
  • Confusing .xls and .xlsx. They use different engines. Modern files need openpyxl, legacy files need xlrd.
  • Installing into the wrong interpreter. If pip install appears to succeed but the import fails, you installed into a different Python than the one VS Code is running. Recheck the selected interpreter in the status bar.
  • Trusting inferred types. Do not assume a column of numbers is numeric until you have checked with df.dtypes. Excel formatting is visual, not data.
  • Ignoring hidden rows and filters. They are still read. If the numbers look too large, hidden content may be included.

Tips

  • Give the source file one clear name and put it beside the script, then refer to it by relative name. Moving both together keeps the script working.
  • Use a virtual environment per project; it makes pip list mean something and makes the setup reproducible later.
  • Reload the file after edits with pd.read_excel rather than trusting an old variable in the debugger.
  • For very large workbooks, loading every sheet takes memory. Read only the sheet you need.
  • Set dtype=str if you want raw cell text untouched, such as when reading codes with leading zeros that Excel and pandas would otherwise strip.
  • Keep a tiny test file with three rows and two columns. It isolates whether a problem is your code or the spreadsheet.

Troubleshooting

If you see ModuleNotFoundError: No module named 'pandas', the package is not installed in the interpreter VS Code is using. Run pip install pandas in the integrated terminal, then check the interpreter in the bottom status bar matches. If you get FileNotFoundError, print import os; print(os.getcwd()) and compare that folder with where the file actually is. If the error names openpyxl, install it; that is the engine, not an optional extra.

If you see an error about the file format or a missing engine for .xls, install xlrd and re-run. A permission error on Windows usually means the file is open in Excel or is read-only in a synced folder; close it or copy it locally. If columns come back as Unnamed: 0 and similar, the header row is wrong, so try header=1 or skiprows. If numbers arrive as text, convert with to_numeric. If the Run button uses a different interpreter than your terminal, reselect the interpreter once so both agree.

If none of that helps, reduce the problem: open a fresh script with only the import and the read call, on a known-good file. That tells you whether the issue is the environment or the specific workbook, and the boundary is where the real fix usually is.

Frequently Asked Questions

Do I need Excel installed to read an Excel file in Python?

No. pandas with openpyxl reads the file directly. You only need a tool to open the spreadsheet yourself if you want to inspect it visually.

Should I use pandas or openpyxl directly?

Use pandas for analysis and data cleaning, because it works on the data as a whole table. Use openpyxl directly when you need cell-level control, such as reading specific cells or preserving formatting and formulas. Formulas are not evaluated by openpyxl; you get the stored formula text, not its result.

How do I read only some columns or rows?

Read the sheet, then use df[["Name", "Total"]] for specific columns, or use_cols=["Name", "Total"] in the read call to skip the rest. For rows, load first and filter with conditions.

Why does VS Code not show my changes after I edit the file?

Python did not re-read the spreadsheet. Re-run the script; the variables from the previous run do not update automatically.

Can I read a password-protected workbook?

If it is encrypted, openpyxl cannot decrypt it. Save an unprotected copy from Excel first and read that.

Where do I put the file so the path works on any computer?

In the same folder as the script, referenced by its bare filename. If you need an absolute path, build it with pathlib.Path(__file__).parent / "data.xlsx" so it stays correct on other machines.