alexa

How do I update column names for all sheets within a file in python ?

How do I update column names for all sheets within a file in python ?

Loop through each header cell, and attempt to assign a new value to each cell, pulling the value from column_name_update_map. If the current header name (header.value) is not in column_name_update_map, a KeyError will be raised that you can safely ignore. In that case, the value is not updated.

 for file in os.listdir("C:/Users/yam/Desktop/hhh/python/sterls"):
    if file.startswith("TVC"):
        wb = load_workbook(file)
        if 'Opt-Ins' in wb.sheetnames:
            wb.remove(wb['Opt-Ins'])
        
        for ws in wb:
            for header in next(ws.rows):
                try:
                    header.value = column_name_update_map[header.value]
                except KeyError:
                    pass
        
        wb.save(file + " (updated headers).xlsx")

354 0
7

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online

Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry