logo
search
Data Import & Export

Fix Python in Excel Cannot Find an Existing Text File

Maira MehtabMaira Mehtab Sep 20, 2026 869 views

Question details

The user is attempting to run a Python script from within Excel to read a text file, but the script fails to find the file despite functioning perfectly in Visual Studio Code.

Product
Microsoft Excel
Device & OS
not provided
Scenario
Trying to import or process local text file data using the Python in Excel feature.
Observed behavior
The script returns a file not found error when executed in Excel, and adjusting to absolute paths or moving the file does not resolve the issue, even though the same code runs fine in an external IDE.
Before you start

Before troubleshooting, understand that Microsoft's native Python in Excel runs in a secure, isolated cloud container, which intentionally restricts direct access to your local computer's hard drive.

Solution 1Recommended

Import the Text File via Power Query

Since Python in Excel cannot directly access local files due to cloud security boundaries, you must first import the data into an Excel table using Power Query, then reference that table in your Python script.

Python in Excel executes your code in an isolated Microsoft Cloud environment. This means standard Python file reading functions (like `open()`) will fail because the cloud container cannot see your local hard drive or network paths.

1
Open Get Data Menu

Open your Excel workbook, navigate to the Data tab on the ribbon, and click on 'Get Data' located in the Get & Transform Data group.

2
Select the Text File

Choose 'From File' and then click 'From Text/CSV'. Locate your target text file in the file explorer and click 'Import'.

3
Load Data into Excel

In the Power Query preview window, verify your data and click 'Load' to bring the text file data into an Excel table on your worksheet.

4
Reference Data in Python

Select a new cell, type `=PY(` to open the Python formula editor, and select the newly created Excel table to process the data securely using Python.

Best Practice: Using Power Query ensures your local data is securely transferred into the Excel grid, making it fully accessible to the Python cloud environment without triggering security restrictions.
Free Microsoft Office alternative

Need a Fast and Reliable Spreadsheet Tool? Try WPS Office

If you are struggling with complex cloud limitations in Microsoft Excel, WPS Spreadsheet offers a lightning-fast, secure, and entirely free alternative for managing and analyzing your data seamlessly on your local machine.

Fully compatible with Microsoft Excel file formats (.xlsx, .xls, .csv).Process large text files and datasets quickly without cloud isolation restrictions.Built-in advanced data processing tools, macros, and charts.Completely free to use with an intuitive, familiar tabbed interface.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my Python script work in VS Code but fail in Excel?

Visual Studio Code runs Python locally on your machine with full access to your local files. Microsoft's native Python in Excel runs in a secure cloud container that intentionally blocks access to your local files and directories.

Can I use absolute paths to force Python in Excel to read my file?

No. Because the Python code is executed in an isolated cloud environment, it cannot resolve local absolute paths (like C:\...) on your physical device, regardless of how they are formatted.

How do I get external text data into Python in Excel?

You must first bring the data into an Excel grid or table. The recommended method is using Power Query to import your Text or CSV files into a sheet, and then referencing those Excel cells in your Python `=PY()` function.