9

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

beebek
  • 1,893
  • 5
  • 29
  • 33

2 Answers2

14

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()
beebek
  • 1,893
  • 5
  • 29
  • 33
-1

You need to put more information in here. You can't just post some code and not say what application you are using, if you are trying to do this in VB Script or some other language, what the issue is, and what you are trying to accomplish. This question doesn't tell anybody anything.

I know VB Script in Excel fairly well and this looks similar. Here is some sample codes that work and might help or give you some ideas in whatever you are trying to do.

For formatting and you can do a lot more than this, but here is some general stuff:

With Target
    .Font.Bold = True
    .Font.Underline = True
    .Font.Size = 11
    .Font.Color = RGB(0, 0, 0)
    .Font.Name = "Calibri"
    .HorizontalAlignment = xlLeft
End With

This tells you how to enter text using the Cells:

Cells(1, 1).Value2 = "Hello World"
ActiveSheet.Cells(1, 1).Value2 = "Hello World"
Worksheets("Sheet1").Cells(1, 1).Value2 = "Hello World"

This does the same thing as the previous code using cells, but these are options for using Range instead:

Range("A1").Value2 = "Hello World"
ActiveSheet.Range("A1").Value2 = "Hello World"
Worksheets("Sheet1").Range("A1").Value2 = "Hello World"

Hopefully that will help in whatever you are trying to do.

Chris
  • 2,679
  • 3
  • 22
  • 23