-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
Support more styles for xlsxwriter #16149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
96ab259
3515f6a
740dca4
9b0ea70
f413cb1
ea3a468
30a8dc4
1c4bcf9
38db7e6
376bc8b
8d63f00
d6441ff
de808df
06b1d7f
26728ee
80ed56a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1596,6 +1596,68 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0, | |
startcol + cell.col, | ||
val, style) | ||
|
||
# Map from openpyxl-oriented styles to flatter xlsxwriter representation | ||
STYLE_MAPPING = [ | ||
(('font', 'name'), 'font_name'), | ||
(('font', 'sz'), 'font_size'), | ||
(('font', 'size'), 'font_size'), | ||
(('font', 'color', 'rgb'), 'font_color'), | ||
(('font', 'color'), 'font_color'), | ||
(('font', 'b'), 'bold'), | ||
(('font', 'bold'), 'bold'), | ||
(('font', 'i'), 'italic'), | ||
(('font', 'italic'), 'italic'), | ||
(('font', 'u'), 'underline'), | ||
(('font', 'underline'), 'underline'), | ||
(('font', 'strike'), 'font_strikeout'), | ||
(('font', 'vertAlign'), 'font_script'), | ||
(('font', 'vertalign'), 'font_script'), | ||
(('number_format', 'format_code'), 'num_format'), | ||
(('number_format',), 'num_format'), | ||
(('protection', 'locked'), 'locked'), | ||
(('protection', 'hidden'), 'hidden'), | ||
(('alignment', 'horizontal'), 'align'), | ||
(('alignment', 'vertical'), 'valign'), | ||
(('alignment', 'text_rotation'), 'rotation'), | ||
(('alignment', 'wrap_text'), 'text_wrap'), | ||
(('alignment', 'indent'), 'indent'), | ||
(('alignment', 'shrink_to_fit'), 'shrink'), | ||
(('fill', 'patternType'), 'pattern'), | ||
(('fill', 'patterntype'), 'pattern'), | ||
(('fill', 'fill_type'), 'pattern'), | ||
(('fill', 'start_color', 'rgb'), 'fg_color'), | ||
(('fill', 'fgColor', 'rgb'), 'fg_color'), | ||
(('fill', 'fgcolor', 'rgb'), 'fg_color'), | ||
(('fill', 'start_color'), 'fg_color'), | ||
(('fill', 'fgColor'), 'fg_color'), | ||
(('fill', 'fgcolor'), 'fg_color'), | ||
(('fill', 'end_color', 'rgb'), 'bg_color'), | ||
(('fill', 'bgColor', 'rgb'), 'bg_color'), | ||
(('fill', 'bgcolor', 'rgb'), 'bg_color'), | ||
(('fill', 'end_color'), 'bg_color'), | ||
(('fill', 'bgColor'), 'bg_color'), | ||
(('fill', 'bgcolor'), 'bg_color'), | ||
(('border', 'color', 'rgb'), 'border_color'), | ||
(('border', 'color'), 'border_color'), | ||
(('border', 'style'), 'border'), | ||
(('border', 'top', 'color', 'rgb'), 'top_color'), | ||
(('border', 'top', 'color'), 'top_color'), | ||
(('border', 'top', 'style'), 'top'), | ||
(('border', 'top'), 'top'), | ||
(('border', 'right', 'color', 'rgb'), 'right_color'), | ||
(('border', 'right', 'color'), 'right_color'), | ||
(('border', 'right', 'style'), 'right'), | ||
(('border', 'right'), 'right'), | ||
(('border', 'bottom', 'color', 'rgb'), 'bottom_color'), | ||
(('border', 'bottom', 'color'), 'bottom_color'), | ||
(('border', 'bottom', 'style'), 'bottom'), | ||
(('border', 'bottom'), 'bottom'), | ||
(('border', 'left', 'color', 'rgb'), 'left_color'), | ||
(('border', 'left', 'color'), 'left_color'), | ||
(('border', 'left', 'style'), 'left'), | ||
(('border', 'left'), 'left'), | ||
] | ||
|
||
def _convert_to_style(self, style_dict, num_format_str=None): | ||
""" | ||
converts a style_dict to an xlsxwriter format object | ||
|
@@ -1610,35 +1672,57 @@ def _convert_to_style(self, style_dict, num_format_str=None): | |
return None | ||
|
||
# Create a XlsxWriter format object. | ||
xl_format = self.book.add_format() | ||
props = {} | ||
|
||
if num_format_str is not None: | ||
xl_format.set_num_format(num_format_str) | ||
props['num_format'] = num_format_str | ||
|
||
|
||
if style_dict is None: | ||
return xl_format | ||
|
||
# Map the cell font to XlsxWriter font properties. | ||
if style_dict.get('font'): | ||
font = style_dict['font'] | ||
if font.get('bold'): | ||
xl_format.set_bold() | ||
|
||
# Map the alignment to XlsxWriter alignment properties. | ||
alignment = style_dict.get('alignment') | ||
if alignment: | ||
if (alignment.get('horizontal') and | ||
alignment['horizontal'] == 'center'): | ||
xl_format.set_align('center') | ||
if (alignment.get('vertical') and | ||
alignment['vertical'] == 'top'): | ||
xl_format.set_align('top') | ||
|
||
# Map the cell borders to XlsxWriter border properties. | ||
if style_dict.get('borders'): | ||
xl_format.set_border() | ||
|
||
return xl_format | ||
return self.book.add_format(props) | ||
|
||
if 'borders' in style_dict: | ||
style_dict = style_dict.copy() | ||
style_dict['border'] = style_dict.pop('borders') | ||
|
||
for src, dst in self.STYLE_MAPPING: | ||
|
||
# src is a sequence of keys into a nested dict | ||
# dst is a flat key | ||
if dst in props: | ||
continue | ||
v = style_dict | ||
for k in src: | ||
try: | ||
v = v[k] | ||
except (KeyError, TypeError): | ||
break | ||
else: | ||
props[dst] = v | ||
|
||
if isinstance(props.get('pattern'), string_types): | ||
# TODO: support other fill patterns | ||
props['pattern'] = 0 if props['pattern'] == 'none' else 1 | ||
|
||
for k in ['border', 'top', 'right', 'bottom', 'left']: | ||
if isinstance(props.get(k), string_types): | ||
try: | ||
props[k] = ['none', 'thin', 'medium', 'dashed', 'dotted', | ||
'thick', 'double', 'hair', 'mediumDashed', | ||
'dashDot', 'mediumDashDot', 'dashDotDot', | ||
'mediumDashDotDot', 'slantDashDot'].\ | ||
index(props[k]) | ||
except ValueError: | ||
props[k] = 2 | ||
|
||
if isinstance(props.get('font_script'), string_types): | ||
props['font_script'] = ['baseline', 'superscript', 'subscript'].\ | ||
index(props['font_script']) | ||
|
||
if isinstance(props.get('underline'), string_types): | ||
props['underline'] = {'none': 0, 'single': 1, 'double': 2, | ||
'singleAccounting': 33, | ||
'doubleAccounting': 34}[props['underline']] | ||
|
||
return self.book.add_format(props) | ||
|
||
|
||
register_writer(_XlsxWriter) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the code would be simpler to make this style formatting into a separate class (rather than have it live in functions sitting in the main excel code). can you refactor to make this cleaner.