gpt4 book ai didi

python - 使用openpyxl更改excel列格式类型

转载 作者:行者123 更新时间:2023-12-04 22:22:29 25 4
gpt4 key购买 nike

我有一个如下所示的数据集;

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

我想用 'general' 格式类型更改这个 'TARİH' 列。

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 列更改为“通用格式类型”。所以我尝试了这个,
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")

关于python - 使用openpyxl更改excel列格式类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61326201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com