text alignment in xlwt with easyxf

beebek picture beebek · Nov 11, 2013 · Viewed 19.6k times · Source

I am using xlwt, excel sheet generation module for python. Basically I am trying to use some styles for the text. The coloring part works fine .i.e.

import xlwt

workbook = xlwt.Workbook(encoding='ascii')
worksheet = workbook.add_sheet('Test sheet')

worksheet.write(0, 0, "Hello World", xlwt.easyxf("pattern: pattern solid, fore_color yellow; font: color white;"))

There was a need to add alignment as well. I found this working

alignment = xlwt.Alignment()
alignment.horz = xlwt.Alignment.HORZ_RIGHT
horz_style = xlwt.XFStyle() 
horz_style.alignment = alignment
worksheet.write(0, 0, "Hello World", horz_style)

But now whole thing is messed up since I can use only either the coloring or alignment.What I am trying is to integrate the alignment feature with xlwt.easyxf as well

Answer

beebek picture beebek · Nov 12, 2013

It was as simple as adding align: horiz right

import xlwt

workbook = xlwt.Workbook(encoding='ascii')
worksheet = workbook.add_sheet('Test sheet')

worksheet.write(0, 0, "Hello World", xlwt.easyxf("pattern: pattern solid, fore_color yellow; font: color white; align: horiz right"))
worksheet.save()