Mukesh Kumar Mishra
Mukesh Kumar Mishra

Reputation: 161

Check if a Excel Sheet is empty

I have an excel workbook with multiple sheets. I need to delete the sheets which are completely empty, as my code when processing finds a blank sheet it fails.

os.chdir(path)
list_file=[]
for file in glob.glob("*.xlsx"):
    print(file)
    list_file.append(file)

I have listed all the files here available.

AB_list=[s for s in list_file if "India" in s]
CD_list=[s for s in list_file if "Japan" in s]

Then, i store the file names is list as per requirement. Now I need to delete empty sheets from those excel files before I move them to a dataframe. Then loop through to read the files into individual dataframe.

Upvotes: 1

Views: 7249

Answers (3)

user8595685
user8595685

Reputation:

You've tagged openpyxl so I assume you're using that.

# workbook is opened MS Exel sheet opened with openpyxl.Workbook
empty_sheets = []
for name in workbook.get_sheet_names():
    sheet = workbook.get_sheet_by_name(name)
    if not sheet.columns():
        empty_sheets.append(sheet)

map(workbook.remove, empty_sheets)

Upvotes: 2

kaza
kaza

Reputation: 2327

ws.max_row and ws.max_column should give you last used cell position. Based on that you can determine if sheet is empty. Also check if this works for you ws.calculate_dimension(), which should return a range.

All the functions are from openpyxl which you are already familiar with.

Upvotes: 2

Kalariya_M
Kalariya_M

Reputation: 1403

you can easily do this with pandas which I'm using too. here

and code looks like

import pandas as pd

df = pd.read_csv(filename) # or

pd.read_excel(filename) for xls file df.empty

Upvotes: 0

Related Questions