Search code examples
python-3.xexcelopenpyxl

Why can't I delete rows in excel files, using openpyxl


I'm doing data science projects, and I'm working with an Excel file that contains the data on temperatures. Long story short, I'm concerned with columns D and E, which they may contain values like '***' or '#'. When I encounter these cells, I want to delete the entire row that contains the cell.

Below shows my code:

from openpyxl import load_workbook

excel = load_workbook('CLMMAXT_TMS_.xlsx').active
for i in reversed(range(2, 10000)):
    value_sharp = excel[f'E{i}'].value
    value_asterisk = excel[f'D{i}'].value
    if value_sharp == '#' or value_asterisk == '***':
        excel.delete_rows(i, amount=1)
        print(f'{i}: {value_asterisk}, {value_sharp}')

I have absolutely no idea why this code does not work. It just didn't delete even ONE row. I have read similar questions and tried their solutions such as the reversed() method, but they didn't work either. Can anyone tell me where I made the mistake and how to fix it? Thanks in advance.


Solution

  • This may sound trivial but you need to save the changes (and your code will work 100% fine).

    from openpyxl import load_workbook
    
    excel = load_workbook('file.xlsx')
    sheet = excel.active
    
    for i in reversed(range(2, 10000)):
        value_sharp = sheet[f'E{i}'].value
        value_asterisk = sheet[f'D{i}'].value
        if value_sharp == '#' or value_asterisk == '***':
            sheet.delete_rows(i, amount=1)
            # print(f'{i}: {value_asterisk}, {value_sharp}')
            
    excel.save('output.xlsx')
    

    Output :

    enter image description here