66
77from __future__ import absolute_import , print_function , unicode_literals
88
9- from docx .enum .text import WD_BREAK
9+ from docx .enum .text import WD_BREAK , WD_UNDERLINE
1010
1111
1212def boolproperty (f ):
@@ -215,12 +215,14 @@ def italic(self):
215215 @property
216216 def underline (self ):
217217 """
218- String name of underline style to be applied. If "none" or False, then no underline
219- is applied. If True, single underline is applied.
218+ Underline text with style of *underline_type*. *underline_type* can
219+ take the values `WD_UNDERLINE.SINGLE`, `WD_UNDERLINE.DOUBLE`, etc.
220+ where `WD_UNDERLINE` is imported from `docx.enum.text`.
220221
221- Valid Values: True, False, 'single', 'words', 'double', 'thick', 'dotted', 'dottedHeavy',
222- 'dash', 'dashedHeavy', 'dashLong', 'dashLongHeavy', 'dotDash', 'dashDotHeavy',
223- 'dotDotDash', 'dashDotDotHeavy', 'wave', 'wavyHeavy', 'wavyDouble', 'none'
222+ Shorthand:
223+ setting to True results in `WD_UNDERLINE.SINGLE`.
224+ setting to False results in `WD_UNDERLINE.NONE`.
225+ setting to None results in `WD_UNDERLINE.NONE`.
224226 """
225227 u = self ._r .get_or_add_rPr ().underline
226228 if u is None :
@@ -231,16 +233,11 @@ def underline(self):
231233 def underline (self , style ):
232234 # interperate style
233235 if style is None :
234- style = 'none'
236+ style = WD_UNDERLINE . NONE
235237 if type (style )== bool :
236- style = 'single' if style else 'none'
237-
238- validStyles = ['single' , 'words' , 'double' , 'thick' , 'dotted' , 'dottedHeavy' , 'dash' ,
239- 'dashedHeavy' , 'dashLong' , 'dashLongHeavy' , 'dotDash' , 'dashDotHeavy' , 'dotDotDash' , 'dashDotDotHeavy' ,
240- 'wave' , 'wavyHeavy' , 'wavyDouble' , 'none' ]
241-
242- if style not in validStyles :
243- raise ValueError ('"' + style + '" is not valid. Needs to be: ' + ', ' .join (validStyles ))
238+ style = WD_UNDERLINE .SINGLE if style else WD_UNDERLINE .NONE
239+
240+ style = WD_UNDERLINE .stringDict [style ]
244241
245242 u = self ._r .get_or_add_rPr ().underline
246243 if u is None :
0 commit comments