Python 使用openpyxl查找文本的颜色
因此,我的应用程序必须找到单元格内文本的值和颜色。我可以找到文本的背景色。例如,我有一个单元格值“Hello我的名字是…很高兴认识你”(粗体文本表示颜色是红色),因此当我找到颜色并看到红色文本时,我想删除它,我知道如何删除它,但不知道如果单元格值为,这并不像可能的那样简单,但如果您遵循逻辑,它对于标准文本颜色格式是有意义的 要识别字体颜色值,需要运行单元格属性: 单元格将返回一个单元格对象Python 使用openpyxl查找文本的颜色,python,python-3.x,openpyxl,Python,Python 3.x,Openpyxl,因此,我的应用程序必须找到单元格内文本的值和颜色。我可以找到文本的背景色。例如,我有一个单元格值“Hello我的名字是…很高兴认识你”(粗体文本表示颜色是红色),因此当我找到颜色并看到红色文本时,我想删除它,我知道如何删除它,但不知道如果单元格值为,这并不像可能的那样简单,但如果您遵循逻辑,它对于标准文本颜色格式是有意义的 要识别字体颜色值,需要运行单元格属性: 单元格将返回一个单元格对象 cell.font将返回字体对象 cell.font.color将返回颜色对象 以下是现有工作簿的REPL
cell.font将返回字体对象
cell.font.color将返回颜色对象 以下是现有工作簿的REPL输出示例:
>> from openpyxl import load_workbook
>> from openpyxl.styles import Font
>> wb = load_workbook('example.xlsx')
>> sheet0 = wb['FEB 2019']
>>> print(sheet0['C20'].font)
<openpyxl.styles.fonts.Font object>
Parameters:
name='Calibri', charset=None, family=None, b=False, i=False, strike=False, outline=None, shadow=None, condense=None, color=<openpyxl.styles.colors.Color object>
Parameters:
rgb='00ffffff', indexed=None, auto=None, theme=None, tint=0.0, type='rgb', extend=None, sz=11.0, u=None, vertAlign=None, scheme=None
>>> print(sheet0['A1'].font.color)
<openpyxl.styles.colors.Color object>
Parameters:
rgb='00ffffff', indexed=None, auto=None, theme=None, tint=0.0, type='rgb'
如果单元格内容为富文本,openpyxl将无法处理该内容。事实证明,将颜色应用到单元格的某个部分需要富文本格式。有关库中的已知问题,请参阅
它可以在xlrd中完成,但只能使用旧的xls格式,而不能使用现代的xlsx。请参阅此stackoverflow答案:(它是python2代码,但很容易转换为python3)(请参见下文-我还添加了行print('color:',segment['font'].color\u index)
,以显示特定的颜色
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import xlrd
# accessing Column 'C' in this example
COL_IDX = 2
book = xlrd.open_workbook('example.xls', formatting_info=True)
first_sheet = book.sheet_by_index(0)
for row_idx in range(first_sheet.nrows):
text_cell = first_sheet.cell_value(row_idx, COL_IDX)
text_cell_xf = book.xf_list[first_sheet.cell_xf_index(row_idx, COL_IDX)]
# skip rows where cell is empty
if not text_cell:
continue
print(text_cell)
text_cell_runlist = first_sheet.rich_text_runlist_map.get((row_idx, COL_IDX))
if text_cell_runlist:
print('(cell multi style) SEGMENTS:')
segments = []
for segment_idx in range(len(text_cell_runlist)):
start = text_cell_runlist[segment_idx][0]
# the last segment starts at given 'start' and ends at the end of the string
end = None
if segment_idx != len(text_cell_runlist) - 1:
end = text_cell_runlist[segment_idx + 1][0]
segment_text = text_cell[start:end]
segments.append({
'text': segment_text,
'font': book.font_list[text_cell_runlist[segment_idx][1]]
})
# segments did not start at beginning, assume cell starts with text styled as the cell
if text_cell_runlist[0][0] != 0:
segments.insert(0, {
'text': text_cell[:text_cell_runlist[0][0]],
'font': book.font_list[text_cell_xf.font_index]
})
for segment in segments:
print(segment['text'])
print('italic:', segment['font'].italic)
print('bold:', segment['font'].bold)
print('colour:', segment['font'].colour_index)
else:
print('(cell single style)')
print('italic:', book.font_list[text_cell_xf.font_index].italic)
print('bold:', book.font_list[text_cell_xf.font_index].bold)
print('colour:', book.font_list[text_cell_xf.font_index].colour_index)
将example.xlsx转换为example.xls,对于单元格C24,这将提供以下输出:
O人,216英镑花园(单元格多样式)段:
O人,216英镑
斜体:0
粗体:1
颜色:8
花园
斜体:0
粗体:1
颜色:10 斜体/粗体0/1可能是布尔值(1为真)
颜色取决于工作簿中存储的颜色图(因为用户可以定义自己的颜色图:).0-7是系统固定的,8+是用户定义的。8默认为用户定义的黑色,10默认为用户定义的红色。当我使用cell.font时,我会获得有关单元格字体的信息,如大小、字体系列、轮廓、阴影,但颜色是无的,因此当我尝试cell.font.color时,我自然不会得到任何颜色。rgb不起作用,但问题是当我查看时在电子表格和我在代码中看到的单元格中,单元格值是“一些文本(该位为黑色),123123sdasd@asdad.com(此位为红色)所以问题是它不能选择颜色,我在全黑单元格中尝试过,但它仍然没有给我所需的黑色输出或十六进制值。如果需要,我可以提供代码。我可以与您共享谷歌驱动器链接。我已经大大减少了文件中的信息,并更改了名称,但想法仍然存在请与Michael Jacksonsanks一起查看手机非常感谢您的帮助我有空时会查看手机,但我相信如果没有,手机会正常工作。我会以不同的方式处理问题。谢谢
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import xlrd
# accessing Column 'C' in this example
COL_IDX = 2
book = xlrd.open_workbook('example.xls', formatting_info=True)
first_sheet = book.sheet_by_index(0)
for row_idx in range(first_sheet.nrows):
text_cell = first_sheet.cell_value(row_idx, COL_IDX)
text_cell_xf = book.xf_list[first_sheet.cell_xf_index(row_idx, COL_IDX)]
# skip rows where cell is empty
if not text_cell:
continue
print(text_cell)
text_cell_runlist = first_sheet.rich_text_runlist_map.get((row_idx, COL_IDX))
if text_cell_runlist:
print('(cell multi style) SEGMENTS:')
segments = []
for segment_idx in range(len(text_cell_runlist)):
start = text_cell_runlist[segment_idx][0]
# the last segment starts at given 'start' and ends at the end of the string
end = None
if segment_idx != len(text_cell_runlist) - 1:
end = text_cell_runlist[segment_idx + 1][0]
segment_text = text_cell[start:end]
segments.append({
'text': segment_text,
'font': book.font_list[text_cell_runlist[segment_idx][1]]
})
# segments did not start at beginning, assume cell starts with text styled as the cell
if text_cell_runlist[0][0] != 0:
segments.insert(0, {
'text': text_cell[:text_cell_runlist[0][0]],
'font': book.font_list[text_cell_xf.font_index]
})
for segment in segments:
print(segment['text'])
print('italic:', segment['font'].italic)
print('bold:', segment['font'].bold)
print('colour:', segment['font'].colour_index)
else:
print('(cell single style)')
print('italic:', book.font_list[text_cell_xf.font_index].italic)
print('bold:', book.font_list[text_cell_xf.font_index].bold)
print('colour:', book.font_list[text_cell_xf.font_index].colour_index)