Commit 11fc3d86 authored by Sanjay Yadav's avatar Sanjay Yadav

ENH: Table cell gradients.

parent 7f3596af
Pipeline #31165 failed with stage
in 53 seconds
......@@ -336,7 +336,8 @@ def table(shape, spec, data):
# Extending table if required.
table_properties.extend_table(shape, data, len(data) + 1, len(data_cols))
# Fetching Table Style for All Cells and texts.
tbl_style = table_properties.get_table_css(shape)
tbl_style = table_properties.get_default_css(shape)
cell_style = table_properties.get_css(spec.get('style', {}), data_cols)
for row_num, row in enumerate(shape.table.rows):
cols = len(row.cells._tr.tc_lst)
......@@ -344,7 +345,6 @@ def table(shape, spec, data):
while cols < len(data_cols):
row.cells._tr.add_tc()
cols += 1
row_type = 'header' if row_num == 0 else 'cell'
for col_num, cell in enumerate(row.cells):
colname = data_cols[col_num]
for paragraph in cell.text_frame.paragraphs:
......@@ -353,21 +353,19 @@ def table(shape, spec, data):
for run in paragraph.runs:
txt = colname if row_num == 0 else data[row_num - 1][colname]
run.text = '{}'.format(txt)
cell_css = spec.get(row_type, {})
if 'text' not in cell_css:
cell_css['text'] = {}
cell_css = {}
txt_css = copy.deepcopy(tbl_style.get('header' if row_num == 0 else 'row', {}))
if row_num > 0 and colname in color_grad:
cell_css = copy.deepcopy(cell_style.get(colname, {}))
grad_txt = scale_data(txt, color_grad[colname]['min'],
color_grad[colname]['max'])
gradient = matplotlib.cm.get_cmap(color_grad[colname]['gradient'])
cell_css['fill'] = matplotlib.colors.to_hex(gradient(grad_txt))
if cell_css.get('fill'):
cell_css['text']['color'] = _color.contrast(cell_css.get('fill'))
txt_css.update(cell_css.get('text', {}))
cell_css.update({'text': txt_css})
table_properties.apply_table_css(cell, paragraph, run, cell_css)
txt_css['color'] = _color.contrast(cell_css.get('fill'))
cell_css['color'] = _color.contrast(cell_css.get('fill'))
txt_css.update(cell_css)
table_properties.apply_table_css(cell, paragraph, run, txt_css)
def oval(shape, spec, data):
......@@ -908,7 +906,8 @@ def custom_table(shape, spec, data):
fourty_five_percent = 0.45
thirty_five_percent = 0.35
fourty_eight_percent = 0.48
tbl_style = table_properties.get_table_css(shape)
tbl_style = table_properties.get_default_css(shape)
cell_style = table_properties.get_css(spec.get('style', {}), data_cols)
for row_num, row in enumerate(shape.table.rows):
cols = len(row.cells._tr.tc_lst)
# Extending cells in newly added rows.
......@@ -917,7 +916,7 @@ def custom_table(shape, spec, data):
cols += 1
_height = row.height + height_margin
y = top + (_height * row_num)
row_type = 'header' if row_num == 0 else 'cell'
for col_num, cell in enumerate(row.cells):
colname = data_cols[col_num]
_width = shape.table.columns[col_num].width
......@@ -964,17 +963,19 @@ def custom_table(shape, spec, data):
run.text = '{}'.format(txt)
paragraph.alignment = PP_ALIGN.RIGHT
cell_css = spec.get(row_type, {})
if 'text' not in cell_css:
cell_css['text'] = {}
cell_css = {}
txt_css = copy.deepcopy(tbl_style.get('header' if row_num == 0 else 'row', {}))
if color_grad and isinstance(txt, (int, float)):
cell_css['fill'] = _color.gradient(txt, color_grad)
if row_num > 0 and colname in color_grad:
cell_css = copy.deepcopy(cell_style.get(colname, {}))
grad_txt = scale_data(txt, color_grad[colname]['min'],
color_grad[colname]['max'])
gradient = matplotlib.cm.get_cmap(color_grad[colname]['gradient'])
cell_css['fill'] = matplotlib.colors.to_hex(gradient(grad_txt))
if cell_css.get('fill'):
cell_css['fill'] = _color.contrast(cell_css.get('fill'))
txt_css.update(cell_css.get('text', {}))
cell_css.update({'text': txt_css})
table_properties.apply_table_css(cell, paragraph, run, cell_css)
txt_css['color'] = _color.contrast(cell_css.get('fill'))
cell_css['color'] = _color.contrast(cell_css.get('fill'))
txt_css.update(cell_css)
table_properties.apply_table_css(cell, paragraph, run, txt_css)
def bar_circle(shape, spec, data):
......
......@@ -577,7 +577,7 @@ class TableProperties():
config.pop(column)
return info
def get_table_css(self, shape):
def get_default_css(self, shape):
"""Function to get Table style for rows and columns."""
pixel_inch = 10000
tbl_style = {}
......@@ -608,26 +608,34 @@ class TableProperties():
return tbl_style
def get_css(self, style, columns):
"""Get CSS from configuration."""
keys = ['font-size', 'fill', 'text-align', 'color', 'font-family',
'bold', 'italic', 'underline']
common_css = {key: style.pop(key) for key in keys if key in style}
for column in columns:
style[column] = {} if column not in style else style[column]
css = copy.deepcopy(common_css)
css.update(style[column])
style[column] = css
return style
def apply_table_css(self, cell, paragraph, run, info):
"""Mak."""
"""Apply Table style."""
pixcel_to_inch = 10000
if info.get('fill'):
cell_fill = cell.fill
cell_fill.solid()
cell_fill.fore_color.rgb = RGBColor.from_string(conver_color_code(info['fill']))
if info.get('text'):
text_style = info.get('text')
if text_style.get('color'):
rows_text = run.font.fill
rows_text.solid()
run.font.color.rgb = RGBColor.from_string(conver_color_code(text_style['color']))
if text_style.get('font-family'):
run.font.name = text_style['font-family']
if text_style.get('font-size'):
run.font.size = pixcel_to_inch * text_style['font-size']
if text_style.get('text-align'):
paragraph.alignment = getattr(PP_ALIGN, text_style['text-align'].upper())
for prop in {'bold', 'italic', 'underline'}:
setattr(run.font, prop, text_style.get(prop))
if info.get('color'):
rows_text = run.font.fill
rows_text.solid()
run.font.color.rgb = RGBColor.from_string(conver_color_code(info['color']))
if info.get('font-family'):
run.font.name = info['font-family']
if info.get('font-size'):
run.font.size = pixcel_to_inch * info['font-size']
if info.get('text-align'):
paragraph.alignment = getattr(PP_ALIGN, info['text-align'].upper())
for prop in {'bold', 'italic', 'underline'}:
setattr(run.font, prop, info.get(prop))
......@@ -10,6 +10,13 @@ edit-table:
gradient:
Sales: {gradient: Reds}
GrossProfit: {gradient: PuBuGn}
# cell:
# text: {'font-size': 14, 'fill': '#ffffff', 'text-align': 'center'}
style:
GrossProfit: {font-size: 14}
font-size: 18
text-align: center
bold: True
italic: True
underline: True
columns: ['Sales', 'GrossProfit']
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment