Openpyxl填充相邻的单元格 [英] Openpyxl fills adjacent cells

查看:112
本文介绍了Openpyxl填充相邻的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一件非常奇怪的事情.当我尝试用纯色填充excel工作表中的单元格时,它会填充相邻的单元格,有时甚至是数十个.奇怪的是,我在代码的其他地方成功地完成了完全相同的事情,只是这一点有所不同.我对可能出了什么问题的想法慢慢消失了,以为也许有人在这里遇到了类似的问题并解决了.我的相关代码是:

__incorrectUnitsColour__='FF33CC'
cell, sheet =GetCellFromDest(str(named_range.destinations),wb) # Returns the cell pointed to by the named_range, this should work fine as I use it a lot in other places.

cell.style.fill.fill_type=openpyxl.style.Fill.FILL_SOLID
cell.style.fill.start_color.index=__incorrectUnitsColour__

如果您对可能出了什么问题有任何想法,我会很高兴听到他们的声音,他们甚至不必为了让我朝着正确的方向思考而工作;)

我在上面的代码上附加了:

print cell.row
print cell.column

,它将按预期返回2 G.我还确认,每运行几次,突出显示单元格的程度就会增加.如果我使用openpyxl突出显示同一工作表中的其他单元格,则会突出显示相同的范围.

我非常强烈地感觉到工作表设置有问题,但是我不知道该怎么办.另外,只有当我尝试以编程方式突出显示同一工作表中的多个单元格时,才会出现此错误.

是什么使所有这些在不同工作表中的突出显示单元格最终变得奇怪,最终导致相同范围的单元格被突出显示.

后续问题:openpyxl是否缓存任何工作表/工作簿数据?因为这可以解释这种行为.

解决方案

我已经进行了很多研究.如果您遇到类似的问题,请按照 解决方案

I've looked into this a lot more. If you're facing a similar issue please follow this question of mine which describes the causes for the above problem. As of now this is a known issue in openpyxl and some people are working on fixing this. So hopefully this bug (because it has been confirmed to be cause by a bug in the library) will be fixed soon.

What I can suggest to try and alleviate the problem is to always select only one cell which is not filled using openpyxl later when saving the workbook in Excel. Also try not to touch the formatted cells too much through Excel itself, just openpyxl. And if you do change the cell format through Excel make sure you only change one cell at a time - if you apply the same style to a range of cells the same range will be then changed when you format a cell using openpyxl.

As unsatisfactory as it is to me that's all I've heard of the authors and figured out myself so far.

这篇关于Openpyxl填充相邻的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆