반응형
XLRD 패키지를 사용한 Excel 시트 셀 색상 코드 식별
xlrd를 사용하여 엑셀 시트에서 데이터를 읽기 위해 python 스크립트를 작성합니다.작업 시트의 셀 중 다른 색상으로 강조 표시된 셀이 거의 없는데 셀의 컬러 코드를 확인하고 싶습니다.그것을 할 수 있는 방법이 있나요?한 가지 예를 들어주시면 감사하겠습니다.
이 문제를 해결하는 방법 중 하나는 다음과 같습니다.
import xlrd
book = xlrd.open_workbook("sample.xls", formatting_info=True)
sheets = book.sheet_names()
print "sheets are:", sheets
for index, sh in enumerate(sheets):
sheet = book.sheet_by_index(index)
print "Sheet:", sheet.name
rows, cols = sheet.nrows, sheet.ncols
print "Number of rows: %s Number of cols: %s" % (rows, cols)
for row in range(rows):
for col in range(cols):
print "row, col is:", row+1, col+1,
thecell = sheet.cell(row, col)
# could get 'dump', 'value', 'xf_index'
print thecell.value,
xfx = sheet.cell_xf_index(row, col)
xf = book.xf_list[xfx]
bgx = xf.background.pattern_colour_index
print bgx
Python-Excel Google 그룹에 대한 자세한 정보.
은 JMax에 합니다.xls
「」이 「」의 경우.xlsx
더 높아진다.★★★★★★★★★★★★★★★★,NotImplementedError: formatting_info=True not yet implemented
Xlrd
가 아직 .xlsx
이다. 그래서 당신은Save As
매번 포맷을 변경할 수 있습니다.
이 있습니다.xlsx
, ""」를 사용한 파일openpyxl
★★★★★★★★★★★★★★★★★★.A2
우리가 알아내야 할 색 코드를 가진 세포입니다.
import openpyxl
from openpyxl import load_workbook
excel_file = 'color_codes.xlsx'
wb = load_workbook(excel_file, data_only = True)
sh = wb['Sheet1']
color_in_hex = sh['A2'].fill.start_color.index # this gives you Hexadecimal value of the color
print ('HEX =',color_in_hex)
print('RGB =', tuple(int(color_in_hex[i:i+2], 16) for i in (0, 2, 4))) # Color in RGB
이 함수는 셀 배경의 rbg 값을 태플 단위로 반환합니다.
def getBGColor(book, sheet, row, col):
xfx = sheet.cell_xf_index(row, col)
xf = book.xf_list[xfx]
bgx = xf.background.pattern_colour_index
pattern_colour = book.colour_map[bgx]
#Actually, despite the name, the background colour is not the background colour.
#background_colour_index = xf.background.background_colour_index
#background_colour = book.colour_map[background_colour_index]
return pattern_colour
# say you have an Excel file called workbook
and inside it there is a worksheet called worksheet
# inside that worksheet is cell say C1 that is highlighted
and you want to get the color value of the highlighted cell
# Trying the openpyxl package
import openpyxl
# Importing all modules from the openpyxl package
from openpyxl import *
# reading ev2 excel workbook through load_workbook function
workbook = load_workbook("C:PATHTO/workbook.xlsx")
# Accessing existing worksheets
worksheet = workbook ["worksheet"]
## METHOD1 ##
# Getting the highlight property of the cell
highlight=str(worksheet ['C1'].fill)
# Printing out the value of the color
index=int(highlight.find("rgb='"))
print(highlight[index+5:index+13])
## METHOD 2 ##
print(worksheet ['C11'].fill.start_color.index)
언급URL : https://stackoverflow.com/questions/7991209/identifying-excel-sheet-cell-color-code-using-xlrd-package
반응형
'it-source' 카테고리의 다른 글
브라우저별로 URL의 최대 길이는 몇 개입니까? (0) | 2023.04.16 |
---|---|
Bash에서 문자열의 마지막 x자 액세스 (0) | 2023.04.16 |
CSS를 사용하여 텍스트를 세로 방향으로 중앙에 배치하려면 어떻게 해야 합니까? (0) | 2023.04.16 |
Bash 디렉토리가 아닌 파일만 나열하는 방법 (0) | 2023.04.11 |
Oracle 11g(클라이언트)을 제거/완전 삭제하는 방법 (0) | 2023.04.11 |