@@ -196,6 +196,11 @@ def text(self):
196196 text += '\n '
197197 return text
198198
199+ @text .setter
200+ def text (self , text ):
201+ self .clear_content ()
202+ _RunContentAppender .append_to_run_from_text (self , text )
203+
199204 @property
200205 def underline (self ):
201206 """
@@ -319,3 +324,59 @@ def val(self, value):
319324
320325 val = WD_UNDERLINE .to_xml (value )
321326 self .set (qn ('w:val' ), val )
327+
328+
329+ class _RunContentAppender (object ):
330+ """
331+ Service object that knows how to translate a Python string into run
332+ content elements appended to a specified ``<w:r>`` element. Contiguous
333+ sequences of regular characters are appended in a single ``<w:t>``
334+ element. Each tab character ('\t ') causes a ``<w:tab/>`` element to be
335+ appended. Likewise a newline or carriage return character ('\n ', '\r ')
336+ causes a ``<w:cr>`` element to be appended.
337+ """
338+ def __init__ (self , r ):
339+ self ._r = r
340+ self ._bfr = []
341+
342+ @classmethod
343+ def append_to_run_from_text (cls , r , text ):
344+ """
345+ Create a "one-shot" ``_RunContentAppender`` instance and use it to
346+ append the run content elements corresponding to *text* to the
347+ ``<w:r>`` element *r*.
348+ """
349+ appender = cls (r )
350+ appender .add_text (text )
351+
352+ def add_text (self , text ):
353+ """
354+ Append the run content elements corresponding to *text* to the
355+ ``<w:r>`` element of this instance.
356+ """
357+ for char in text :
358+ self .add_char (char )
359+ self .flush ()
360+
361+ def add_char (self , char ):
362+ """
363+ Process the next character of input through the translation finite
364+ state maching (FSM). There are two possible states, buffer pending
365+ and not pending, but those are hidden behind the ``.flush()`` method
366+ which must be called at the end of text to ensure any pending
367+ ``<w:t>`` element is written.
368+ """
369+ if char == '\t ' :
370+ self .flush ()
371+ self ._r .add_tab ()
372+ elif char in '\r \n ' :
373+ self .flush ()
374+ self ._r .add_cr ()
375+ else :
376+ self ._bfr .append (char )
377+
378+ def flush (self ):
379+ text = '' .join (self ._bfr )
380+ if text :
381+ self ._r .add_t (text )
382+ del self ._bfr [:]
0 commit comments