[Question] set_cell_color
Created originally on Bitbucket by HorationCaine
Hi, I am currently learning openpyxl, this library is great, but I have a problem
I want to use openpyxl to implement the function of changing the background color of a cell.
Office Excel + python3.6 + openpyxl 3.0.0
import openpyxl
from openpyxl.styles import PatternFill, colors
path = 'C:/Users/ABC/Desktop/123.xlsx'
wb = openpyxl.load_workbook(path)
sheet = 0
cell = 'A1'
sht = wb.worksheets[sheet]
sht[cell].fill = PatternFill(fill_type='solid', fgColor="00000000") # BLACK
wb.save(path)
The background color of the cell successfully turned black.
BUT, next I want to get the background color of the cell.
path = 'C:/Users/ABC/Desktop/123.xlsx'
sheet = 0
wb = openpyxl.load_workbook(path)
sht = wb.worksheets[sheet]
cc = sht['A1'].fill # the background color of the cell : BLACK
print("A1: ",cc)
print('-*'*5)
cc = sht["A3"].fill # Manually change the cell background color to black
print("A3: ",cc)
print('-*'*5)
cc = sht["A5"].fill # No change, it is the default background color
print("A5: ",cc)
The result is:
A1: <openpyxl.styles.fills.PatternFill object>
Parameters:
patternType='solid', fgColor=<openpyxl.styles.colors.Color object>
Parameters:
rgb='00000000', indexed=None, auto=None, theme=None, tint=0.0, type='rgb', bgColor=<openpyxl.styles.colors.Color object>
Parameters:
rgb='00000000', indexed=None, auto=None, theme=None, tint=0.0, type='rgb'
-*-*-*-*-*
A3: <openpyxl.styles.fills.PatternFill object>
Parameters:
patternType='solid', fgColor=<openpyxl.styles.colors.Color object>
Parameters:
rgb=None, indexed=None, auto=None, theme=1, tint=0.0, type='theme', bgColor=<openpyxl.styles.colors.Color object>
Parameters:
rgb=None, indexed=64, auto=None, theme=None, tint=0.0, type='indexed'
-*-*-*-*-*
A5: <openpyxl.styles.fills.PatternFill object>
Parameters:
patternType=None, fgColor=<openpyxl.styles.colors.Color object>
Parameters:
rgb='00000000', indexed=None, auto=None, theme=None, tint=0.0, type='rgb', bgColor=<openpyxl.styles.colors.Color object>
Parameters:
rgb='00000000', indexed=None, auto=None, theme=None, tint=0.0, type='rgb'
I found that the black of the code setting(A1) and the black of the manual setting(A3) get different results.
The black setting of the code setting(A1) is the same as the default background color(A5).
I am very confused,
Is it because I don’t know enough? I hope you can take the time to reply to me.
print(“Thank you“)