gpt4 book ai didi

python - openpyxl: AttributeError: 'MergedCell' 对象属性 'value' 是只读的

转载 作者:行者123 更新时间:2023-12-03 20:58:21 30 4
gpt4 key购买 nike

当我尝试在现有 .xlsx 文件中填充单元格,然后将其保存到一个新文件时,我收到消息:

import openpyxl

path = "/home/karol/Dokumenty/wzor.xlsx"
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active
new_protokol = sheet_obj


firma = input("Podaj nazwe: ")
nazwa_pliku = "Protokol odczytu"
filename = nazwa_pliku + firma + ".xlsx"



sheet_obj["C1"] = firma
sheet_obj["D1"] = input()

new_protokol.save(filename=filename)

Traceback (most recent call last):
File "/home/karol/PycharmProjects/Protokolu/Main.py", line 16, in <module>
sheet_obj["C1"] = firma
File "/home/karol/PycharmProjects/Protokolu/venv/lib/python3.7/site-packages/openpyxl/worksheet/worksheet.py", line 309, in __setitem__
self[key].value = value
AttributeError: 'MergedCell' object attribute 'value' is read-only

Process finished with exit code 1

如何解决?

最佳答案

当您合并单元格时,除了左上角的所有单元格都将从工作​​表中删除。携带合并单元格的边框信息,边界单元合并单元格创建为 MergeCells,其值始终为 'None'

ws.merge_cells('B2:F4')
top_left_cell = ws['B2']
top_left_cell.value = "My Cell"
请尝试这种方法,它对您来说效果很好。

关于python - openpyxl: AttributeError: 'MergedCell' 对象属性 'value' 是只读的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59663517/

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