excel 使用 openpyxl 更改列格式类型

excel column format type change with openpyxl

我有如下数据集;

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
.
.

我想将此 'TARİH' 列更改为 'general' 格式类型。

Openpyxl 让我改变一个单元格,

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
.
.

我想将整个 D 列更改为 "general format type"。所以我试了这个,

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")

出现这个错误,

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

Openpyxl 不允许我更改所有列。如何更改所有列?

我认为你必须遍历可能的行。

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")