OuncesBounces
OuncesBounces

Reputation: 27

Can't open an Excel file using openpyxl

Code:

import openpyxl as opxl
wb = opxl.load_workbook('Food.xlsx')

Here is Food.xlsx in flder, but nothing happened.

Error:

FileNotFoundError: [Errno 2] No such file or directory: 'Food.xlsx'

Also, when I use full path method, it returns yet another error. Code:

import openlyxl as opxl 
wb = opxl.load_workbook(C:/Users/adminname/Desktop/Workplace/Folder/MiniFolder/FileName)

And I got a bad zip file error.

Upvotes: 0

Views: 1817

Answers (1)

Sy Ker
Sy Ker

Reputation: 2190

Two options;

1. Input the full path to your file;

import openpyxl as opxl

wb = opxl.load_workbook('full_path/to_your/folder/Food.xlsx')

2. Change your script's working directory;

import os
import openpyxl as opxl

# change working directory
os.chdir('full_path/to_your/folder/')

# check the current working directory
print(os.getcwd())

# load your file by name 
wb = opxl.load_workbook('Food.xlsx')

Upvotes: 1

Related Questions