मैं एक एक्सेल शीट से डेटा पढ़ने के लिए एक पायथन स्क्रिप्ट लिख रहा हूं xlrd। कार्य पत्र की कोशिकाओं के कुछ अलग रंग के साथ प्रकाश डाला गया है और मैं सेल के रंग कोड की पहचान करना चाहता हूं। क्या उसे करने का कोई तरीका है ? एक उदाहरण वास्तव में सराहना की जाएगी।
उत्तर:
उत्तर № 1 के लिए 37इसे संभालने का एक तरीका यह है:
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
पर अधिक जानकारी पायथन-एक्सेल Google समूह.
उत्तर № 2 के लिए 1
यह फ़ंक्शन टपल में सेल बैकग्राउंड की आरजीबी वैल्यू लौटाता है।
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