Skip to content

Commit 1871043

Browse files
Benjamin-TBenjamin-T
authored andcommitted
fixed issue where it wasn't possible to incorperate newline character in caption text
1 parent d5dd051 commit 1871043

5 files changed

Lines changed: 36 additions & 4 deletions

File tree

docx/document.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,12 @@ def create_caption(self, entry, paragraph, obj_type, bmark='1'):
199199
bmrk_1.set(qn('w:id'), '1')
200200
par.append(bmrk_1)
201201

202-
run = paragraph.add_run()
203202
caption = OxmlElement('w:t')
204203
caption.set(qn('xml:space'), 'preserve')
205204
caption.text = ' {:s}'.format(entry)
206-
run._r.append(caption)
205+
run = paragraph.add_run(caption.text)
206+
207+
#run._r.append(caption)
207208

208209
def add_caption(self, caption, bmark, obj_type='figure'):
209210
"""

docx/oxml/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def clear_content(self):
401401
if tcPr is not None:
402402
new_children.append(tcPr)
403403
self[:] = new_children
404-
404+
405405
@property
406406
def grid_span(self):
407407
"""

docx/oxml/text/run.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ..xmlchemy import (
1010
BaseOxmlElement, OptionalAttribute, ZeroOrMore, ZeroOrOne
1111
)
12-
12+
from docx.oxml import OxmlElement
1313

1414
class CT_Br(BaseOxmlElement):
1515
"""
@@ -43,6 +43,17 @@ def add_t(self, text):
4343
t.set(qn('xml:space'), 'preserve')
4444
return t
4545

46+
def add_bookmark_start(self, name='test_bookmark'):
47+
bmrk = OxmlElement('w:bookmarkStart')
48+
bmrk.set(qn('w:id'), '1')
49+
bmrk.set(qn('w:name'), name)
50+
return bmrk
51+
52+
def add_bookmark_end(self):
53+
bmrk = OxmlElement('w:bookmarkEnd')
54+
bmrk.set(qn('w:id'), '1')
55+
return bmrk
56+
4657
def add_drawing(self, inline_or_anchor):
4758
"""
4859
Return a newly appended ``CT_Drawing`` (``<w:drawing>``) child

docx/table.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,22 @@ def __init__(self, tc, parent):
196196
super(_Cell, self).__init__(tc, parent)
197197
self._tc = self._element = tc
198198

199+
def add_color(self, color_hex):
200+
from docx.oxml import OxmlElement
201+
from docx.oxml.ns import qn
202+
203+
204+
a = self._tc.get_or_add_tcPr()
205+
#<w:shd w:val="clear" w:color="auto" w:fill="5D78CD" w:themeFill="text2" w:themeFillTint="99" />
206+
color = OxmlElement('w:shd')
207+
color.set(qn('w:val'), "clear")
208+
color.set(qn('w:color'), "manual" )
209+
color.set(qn('w:fill'), color_hex)
210+
#color.set(qn('w:themeFill'), "text2")
211+
color.set(qn('w:themeFillTint'), "99")
212+
a.append(color)
213+
return a
214+
199215
def add_paragraph(self, text='', style=None):
200216
"""
201217
Return a paragraph newly added to the end of the content in this

docx/text/run.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ def add_text(self, text):
8080
t = self._r.add_t(text)
8181
return _Text(t)
8282

83+
def add_bookmark(self, name='test_bookmark'):
84+
self._r.append(self._r.add_bookmark_start(name=name))
85+
self._r.append(self._r.add_bookmark_end())
86+
8387
@property
8488
def bold(self):
8589
"""

0 commit comments

Comments
 (0)