不同颜色的细胞具有相同的bg颜色值

2024-05-13 15:13:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些基本上是表格的电子表格,由生产人员打印、用笔书写,然后数字化以供分析。我正在用openpyxl编写一个脚本,用于收集数字化数据进行分析。目前,唯一的分析是“这个框实际多久填写一次而不是留空?”。你知道吗

这些表单会随着我们的制造过程/需求等的变化而发生变化,因此每次表单发生变化时都要修改脚本,这是一项艰巨的工作。我们生产的每个零件的形式都不一样。你知道吗

我认为一个简单的解决方案是要求电子表格中所有不打算写入的单元格都有颜色,这样脚本就可以挑出所有白色单元格来收集数据。你知道吗

然而,据我所知,这些工具似乎不能很好地识别细胞颜色。下图是工作表的外观(没有任何文本)

Text-free sample spreadsheet

我的代码:

wb = load_workbook( 'template.xlsx' )
ws = wb.active

print( "A4 bgColor" )
print( ws['A4'].fill.bgColor )
print( "" )

print( "E7 bgColor" )
print( ws['E7'].fill.bgColor )
print( "" )

print( "E8 bgColor" )
print( ws['E8'].fill.bgColor )
print( "" )

print( "F7 bgColor" )
print( ws['F7'].fill.bgColor )
print( "" )

print( "A28 bgColor" )
print( ws['A28'].fill.bgColor )
print( "" )

输出:

A4 bgColor
<openpyxl.styles.colors.Color object>
Parameters:
rgb=None, indexed=64, auto=None, theme=None, tint=0.0, type='indexed'

E7 bgColor
<openpyxl.styles.colors.Color object>
Parameters:
rgb='00000000', indexed=None, auto=None, theme=None, tint=0.0, type='rgb'

E8 bgColor
<openpyxl.styles.colors.Color object>
Parameters:
rgb=None, indexed=64, auto=None, theme=None, tint=0.0, type='indexed'

F7 bgColor
<openpyxl.styles.colors.Color object>
Parameters:
rgb='00000000', indexed=None, auto=None, theme=None, tint=0.0, type='rgb'

A28 bgColor
<openpyxl.styles.colors.Color object>
Parameters:
rgb=None, indexed=64, auto=None, theme=None, tint=0.0, type='indexed'

A4和A28被设置成某种形式的灰色,我认为这是MS Excel中的主题色。具体来说,Gray, Accent 3, Lighter 60%。E7和F7被设置为No Fill,E8被设置为White, Background 1

问题是细胞A4和E8报告它们的颜色之间没有差异。他们都用的是颜色指数64,据我所知,这不是真的。即使是这样,同一个索引也表示两种不同的颜色,那么给出什么呢?你知道吗

我宁愿不要求单元格是特别的No Fill,因为它要求任何对这些表进行更改的人都知道白色!实际上是白色的。你知道吗


编辑:

一个建议让我更仔细地观察整个fill对象,我看到fgColor做了一些不同的事情。改为使用fgColor的新输出:

A4 fgColor
<openpyxl.styles.colors.Color object>
Parameters:
rgb=None, indexed=None, auto=None, theme=6, tint=0.5999938962981048, type='theme'

E7 fgColor
<openpyxl.styles.colors.Color object>
Parameters:
rgb='00000000', indexed=None, auto=None, theme=None, tint=0.0, type='rgb'

E8 fgColor
<openpyxl.styles.colors.Color object>
Parameters:
rgb=None, indexed=None, auto=None, theme=0, tint=0.0, type='theme'

F7 fgColor
<openpyxl.styles.colors.Color object>
Parameters:
rgb='00000000', indexed=None, auto=None, theme=None, tint=0.0, type='rgb'

A28 fgColor
<openpyxl.styles.colors.Color object>
Parameters:
rgb=None, indexed=None, auto=None, theme=6, tint=0.5999938962981048, type='theme'

theme=6对应于颜色选择器的第七列中的所选灰色,tint=0.59999...对应于excel提到的“60%较浅”的东西。你知道吗

我想应该看前景而不是背景。。。你知道吗


Tags: noneautoobjecttypergbthemeindexedcolor