views:

1687

answers:

2

hi , i am using pyexcelerator python module to generate excel files, i want to apply bold style to part of cell text, but not to the whole cell. how to do it ?

+1  A: 

This is an example from Excel documentation:

With Worksheets("Sheet1").Range("B1")
    .Value = "New Title"
    .Characters(5, 5).Font.Bold = True
End With

So the Characters property of the cell you want to manipulate is the answer to your question. It's used as Characters(start, length).

PS: I've never used the module in question, but I've used Excel COM automation in python scripts. The Characters property is available using win32com.

ΤΖΩΤΖΙΟΥ
Thanks for the reply, i actually want to know if this is possible using python language , excel supports it for sure , but i couldn't find any way to do it using python.
Ashish
i won't be able to use it as i am using ubuntu , what i was looking for was may be some neat hack in pyexcelerrator module , because it does gives back biff record data , and other records applicable to cell.anyway i will give it a try if i am able to expose it as a web service.
Ashish
+2  A: 

Found example here: Generate an Excel Formatted File Right in Python

Notice that you make a font object and then give it to a style object, and then provide that style object when writing to the sheet:

import pyExcelerator as xl

def save_in_excel(headers,values):
    #Open new workbook
    mydoc=xl.Workbook()
    #Add a worksheet
    mysheet=mydoc.add_sheet("test")
    #write headers
    header_font=xl.Font() #make a font object
    header_font.bold=True
    header_font.underline=True
    #font needs to be style actually
    header_style = xl.XFStyle(); header_style.font = header_font
    for col,value in enumerate(headers):
        mysheet.write(0,col,value,header_style)
    #write values and highlight those that match my criteria
    highlighted_row_font=xl.Font() #no real highlighting available?
    highlighted_row_font.bold=True
    highlighted_row_font.colour_index=2 #2 is red,
    highlighted_row_style = xl.XFStyle(); highlighted_row_style.font = highlighted_row_font
    for row_num,row_values in enumerate(values):
        row_num+=1 #start at row 1
        if row_values[1]=='Manatee':
            for col,value in enumerate(row_values):
                #make Manatee's (sp) red
                mysheet.write(row_num,col,value,highlighted_row_style)
        else:
            for col,value in enumerate(row_values):
                #normal row
                mysheet.write(row_num,col,value)
    #save file
    mydoc.save(r'C:testpyexel.xlt')

headers=['Date','Name','Localatity']
data=[
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Manatee','San Jose'],
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Manatee','San Jose'],
]

save_in_excel(headers,data)
Greg
thanks for the reply..but this code will highlight the entire cell text , or in other words it will apply style to entire cell,while i want to highlight only specific words in the cell.
Ashish