[snip]
> You will notice that it is using a whole new xf for the column-B
> cells, and a whole new font for the bold text in those cells.
>
> There are currently no APIs to get the existing xf, and to get a clone
> of the existing font so that "bold" can be imposed on it. These are
> "under contemplation". Is what you want to do a one-off exercise or a
> regular one?
I stopped contemplating and used existing functionality:
xlutils.copy.copy2() provides a mapping from xlrd xf_index to the
corresponding xlwt xf. That's the "get the existing xf" part --
indirect, not dragged out of the xlwt object model, but definitely
got.
Getting a clone of an existing object of most usefile types is done
easily with copy.deepcopy().
Here's the code:
from xlrd import open_workbook
import xlutils.copy
import copy
from sys import argv
rb = open_workbook(argv[1], formatting_info=True)
rs = rb.sheet_by_index(0)
# Get an xlwt workbook, plus a list which provides a mapping from
# xlrd xf_index to the corresponding xlwt xf.
wb, wxfs = xlutils.copy.copy2(rb)
ws = wb.get_sheet(0)
for row_index in xrange(rs.nrows):
rvalue = rs.cell(row_index, 1).value
# Get the cell's xlrd xf_index
rxf_index = rs.cell(row_index, 1).xf_index
# Get the xlwt xf that's used for the output cell
wxf = wxfs[rxf_index]
# We must not update the existing font object; it is used for
# the non-bold part of the text.
new_font = copy.deepcopy(wxf.font)
new_font.bold = True
myword = rvalue.split(' ')[0]
rich_text = [(myword, new_font), rvalue[len(myword):]]
ws.write_rich_text(row_index, 1, rich_text, wxf)
wb.save(argv[2])