Skip to content

Commit 815acd8

Browse files
committed
Fixed #25 - Paragraph and character styling is not serialized correctly
1 parent 827f419 commit 815acd8

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

ooxml/serialize.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ def serialize_paragraph(ctx, document, par, root, embed=True):
560560
if isinstance(el, doc.Text):
561561
children = list(elem)
562562
_text_style = get_style_css(ctx, el)
563-
_text_class = el.rpr.get('style', '')
563+
_text_class = el.rpr.get('style', '').lower()
564564

565565
if get_style_fontsize(el) > max_font_size:
566566
max_font_size = get_style_fontsize(el)
@@ -599,15 +599,16 @@ def _add_formatting(f, new_element, _element):
599599
else:
600600
new_element = etree.Element('span')
601601
new_element.text = el.value()
602+
602603
if ctx.options['embed_styles']:
603604
try:
604-
new_element.set('class', el.rpr['style'].lower())
605+
new_element.set('class', _text_class)
605606
except:
606607
pass
607608

608609
for comment_id in ctx.opened_comments:
609-
document.comments[comment_id].text += ' ' + el.value()
610-
610+
if comment_id in document.comments:
611+
document.comments[comment_id].text += ' ' + el.value()
611612

612613
if ctx.options['embed_styles']:
613614
if _text_style != '' and _style != _text_style:
@@ -622,7 +623,7 @@ def _add_formatting(f, new_element, _element):
622623

623624
if len(children) > 0:
624625
_child_style = children[-1].get('style') or ''
625-
_child_class = new_element.get('class', '')
626+
_child_class = children[-1].get('class', '')
626627

627628
if new_element.tag == children[-1].tag and ((_text_class == _child_class or _child_class == '') and (_text_style == _child_style or _child_style == '')) and children[-1].tail is None:
628629
txt = children[-1].text or ''
@@ -637,15 +638,26 @@ def _add_formatting(f, new_element, _element):
637638
txt = _e.tail or ''
638639
_e.tail = u'{}{}'.format(txt, new_element.text)
639640
was_inserted = True
641+
642+
if not was_inserted and new_element.tag == 'span' and (_text_class != _child_class):
643+
_e = children[-1]
644+
txt = _e.tail or ''
645+
_e.tail = u'{}{}'.format(txt, new_element.text)
646+
was_inserted = True
640647

641648
if not was_inserted:
642649
_child_class = new_element.get('class', '')
650+
try:
651+
_child_class = children[-1].get('class', '')
652+
except:
653+
_child_class = ''
643654

644-
if _style == _text_style and new_element.tag == 'span' and (_text_class == _child_class or _child_class == '') :
655+
if _style == _text_style and new_element.tag == 'span' and (_text_class == _child_class):
645656
txt = elem.text or ''
646657
elem.text = u'{}{}'.format(txt, new_element.text)
647658
else:
648-
elem.append(new_element)
659+
if new_element.text != u'':
660+
elem.append(new_element)
649661

650662
if not par.is_dropcap() and par.ilvl == None:
651663
if style:

0 commit comments

Comments
 (0)