piseynir
piseynir

Reputation: 225

excel column format type change with openpyxl

I have a dataset like below;

TARİH (column name)
05.01.2020  00:00:00
05.01.2020  00:00:00
05.01.2020  00:00:00
05.01.2020  00:00:00
.
.

I want to change this 'TARİH' column with 'general' format type.

Openpyxl let me to change one cell,

import openpyxl

wb = openpyxl.load_workbook("D:\Documents\Desktop\deneme/2020 Data_çalışma.xlsx")
ws = wb["Sheet1"]
ws['D2'].number_format = 'General'
wb.save("D:\Documents\Desktop\deneme/2020 Data_çalışma.xlsx")

Output:

TARİH (column name)
43835
05.01.2020  00:00:00
05.01.2020  00:00:00
05.01.2020  00:00:00
.
.

I want to change whole D column to "general format type". So I tried this one,

import openpyxl

wb = openpyxl.load_workbook("D:\Documents\Desktop\deneme/2020 Data_çalışma.xlsx")
ws = wb["Sheet1"]
ws['D'].number_format = 'General'
wb.save("D:\Documents\Desktop\deneme/2020 Data_çalışma.xlsx")

This error occured,

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

Openpyxl don't let me to change all column. How can I change all column?

Upvotes: 1

Views: 4596

Answers (1)

Sri
Sri

Reputation: 2318

I think you have to loop over the possible rows.

import openpyxl

wb = openpyxl.load_workbook("D:\Documents\Desktop\deneme/2020 Data_çalışma.xlsx")
ws = wb["Sheet1"]
for row in range(2, ws.max_row+1):
    ws["{}{}".format("D", row)].number_format = 'General'
wb.save("D:\Documents\Desktop\deneme/2020 Data_çalışma.xlsx")

Upvotes: 3

Related Questions