Home > front end >  Change number format using headers - openpyxl
Change number format using headers - openpyxl

Time:12-14

I have an Excel file in which I want to convert the number formatting from 'General' to 'Date'. I know how to do so for one column when referring to the column letter:

workbook = openpyxl.load_workbook('path\filename.xlsx')
worksheet = workbook['Sheet1']

for row in range(2, worksheet.max_row 1):
    ws["{}{}".format(ColNames['Report_date'], row)].number_format='yyyy-mm-dd;@'

As you can see, I now use the column letter "D" to point out the column that I want to be formatted differently. Now, I would like to use the header in row 1 called "Start_Date" to refer to this column. I tried a method from the following post to achieve this: select a column by its name - openpyxl. However, that resulted in a KeyError: "Start_Date":

# Create a dictionary of column names
ColNames = {}
Current = 0
for COL in worksheet.iter_cols(1, worksheet.max_column):
    ColNames[COL[0].value] = Current
    Current  = 1

for row in range(2, worksheet.max_row 1):
    ws["{}{}".format(ColNames['Start_Date'], row)].number_format='yyyy-mm-dd;@'

EDIT This method results in the following error:

AttributeError: 'tuple' object has no attribute 'number_format'

Additionally, I have more columns from which the number formatting needs to be changed. I have a list with the names of those columns:

DateColumns = ['Start_Date', 'End_Date', 'Birthday']

Is there a way that I can use the list DateColumns so that I can save some lines of code?

Thanks in advance.

Please note that I posted a similar question earlier. The following post was referred to as an answer Python: Simulating CSV.DictReader with OpenPyXL. However, I don't see how the answers in that post can be adjusted to my needs.

CodePudding user response:

You need to know which columns you want to change the number format on which you have conveniently put into a list, so why not just use that list.
Get the headers in your sheet, check if the Header is in the DateColumns list, if so then update all the entries in that column from row 2 to max with the date format you want...

...
DateColumns = ['Start_Date', 'End_Date', 'Birthday']

for COL in worksheet.iter_cols(min_row=1,max_row=1):
    header = COL[0]
    if header.value in DateColumns:
        for row in range(2, worksheet.max_row 1):
            worksheet.cell(row, COL[0].column).number_format='yyyy-mm-dd;@'
  • Related