|
18 | 18 | PYDOCX_STYLES, |
19 | 19 | TWIPS_PER_POINT, |
20 | 20 | EMUS_PER_PIXEL, |
| 21 | + BRIGHTNESS_DARK_COLOR, |
| 22 | + COLOR_FOR_DARK_BACKGROUND |
21 | 23 | ) |
22 | 24 | from pydocx.export.base import PyDocXExporter |
23 | 25 | from pydocx.export.numbering_span import NumberingItem |
24 | 26 | from pydocx.openxml import wordprocessing |
| 27 | +from pydocx.openxml.wordprocessing.paragraph import Paragraph |
| 28 | +from pydocx.openxml.wordprocessing.table_cell import TableCell |
| 29 | +from pydocx.util import color |
25 | 30 | from pydocx.util.uri import uri_is_external |
26 | 31 | from pydocx.util.xml import ( |
27 | 32 | convert_dictionary_to_html_attributes, |
@@ -96,12 +101,12 @@ class HtmlTag(object): |
96 | 101 | closed_tag_format = '</{tag}>' |
97 | 102 |
|
98 | 103 | def __init__( |
99 | | - self, |
100 | | - tag, |
101 | | - allow_self_closing=False, |
102 | | - closed=False, |
103 | | - allow_whitespace=False, |
104 | | - **attrs |
| 104 | + self, |
| 105 | + tag, |
| 106 | + allow_self_closing=False, |
| 107 | + closed=False, |
| 108 | + allow_whitespace=False, |
| 109 | + **attrs |
105 | 110 | ): |
106 | 111 | self.tag = tag |
107 | 112 | self.allow_self_closing = allow_self_closing |
@@ -486,6 +491,29 @@ def export_run_property_color(self, run, results): |
486 | 491 | tag = HtmlTag('span', **attrs) |
487 | 492 | return self.export_run_property(tag, run, results) |
488 | 493 |
|
| 494 | + def export_run_property_parent_background_color(self, run, results): |
| 495 | + background_color = None |
| 496 | + |
| 497 | + if isinstance(run.parent, (Paragraph,)): |
| 498 | + paragraph = run.parent |
| 499 | + if isinstance(paragraph.parent, (TableCell,)) and paragraph.parent.properties: |
| 500 | + table_cell_prop = paragraph.parent.properties |
| 501 | + background_color = table_cell_prop.background_color |
| 502 | + |
| 503 | + if background_color: |
| 504 | + brightness = color.brightness(background_color) |
| 505 | + # We need to change the text color if background color is dark |
| 506 | + # and text run does not have custom color. |
| 507 | + # Office Word does this automatically. |
| 508 | + if brightness < BRIGHTNESS_DARK_COLOR: |
| 509 | + attrs = { |
| 510 | + 'style': 'color: #%s' % COLOR_FOR_DARK_BACKGROUND |
| 511 | + } |
| 512 | + tag = HtmlTag('span', **attrs) |
| 513 | + results = self.export_run_property(tag, run, results) |
| 514 | + |
| 515 | + return results |
| 516 | + |
489 | 517 | def export_text(self, text): |
490 | 518 | results = super(PyDocXHTMLExporter, self).export_text(text) |
491 | 519 | for result in results: |
@@ -582,6 +610,12 @@ def export_table_cell(self, table_cell): |
582 | 610 | attrs['colspan'] = colspan |
583 | 611 | if rowspan > 1: |
584 | 612 | attrs['rowspan'] = rowspan |
| 613 | + |
| 614 | + if table_cell.properties: |
| 615 | + background_color = table_cell.properties.background_color |
| 616 | + if background_color: |
| 617 | + attrs['style'] = 'background-color: #%s' % background_color |
| 618 | + |
585 | 619 | tag = HtmlTag('td', **attrs) |
586 | 620 |
|
587 | 621 | numbering_spans = self.yield_numbering_spans(table_cell.children) |
|
0 commit comments