Back to SDS/2 Parametric Scripts

 

##  columnize.py Version 1.00 (module macrolib.columnize)

##  Copyright (c) 2007 Bruce Vaughan, BV Detailing & Design, Inc.

##  All rights reserved.

##  NOT FOR SALE. The software is provided "as is" without any warranty.

############################################################################

 

"""

Pad a text string on the right, left, or each end to fill the column width

argument. Credit to Barton

"""

 

def columnize(word, width, align='Left'):

    nSpaces = width - len(word)

    if nSpaces < 0:

        nSpaces = 0

    if align == 'Left':

        return word + (" " * nSpaces)

    if align == 'Right':

        return (" " * nSpaces) + word

    return (" " * (nSpaces/2)) + word + (" " * (nSpaces-nSpaces/2))

 

 

if __name__ == '__main__':

    print repr((columnize('This text is left aligned', 40)))

    print repr((columnize('This text is right aligned', 40, 'Right')))

    print repr((columnize('This text is center aligned', 40, 'Center')))

 

'''

>>> 'This text is left aligned               '

'              This text is right aligned'

'      This text is center aligned       '

>>>

'''