简体   繁体   中英

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.

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)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM