First word must be bold

314 views
Skip to first unread message

AntonS Sokol

unread,
Jan 23, 2011, 3:49:41 PM1/23/11
to python-excel
Hello.I want to edit the xsl column that first and only first word
must be formatted as bold.
This code setting all cells to bold font, but I need only the first
word.Can anyone help me?
from xlrd import open_workbook
from xlwt import easyxf
from xlutils.copy import copy
rb = open_workbook('date.xls',formatting_info=True)
rs = rb.sheet_by_index(0)
wb = copy(rb)
ws = wb.get_sheet(0)
for row_index in range(rs.nrows):
a= rs.cell(row_index,1).value
myword=rs.cell(row_index,1).value.split(' ')[0]
out=
ws.write(row_index,1,myword,easyxf(
'font: bold true'))

wb.save('output1.xls')

John Machin

unread,
Jan 23, 2011, 6:45:18 PM1/23/11
to python-excel


On Jan 24, 7:49 am, AntonS Sokol <ti.poln...@gmail.com> wrote:
> Hello.I want to edit the xsl column that first and only first word
> must be formatted as bold.

Hi, Anton

This can't be done with version 0.7.2 of xlwt.

The next version (nearing release, and available from svn (https://
secure.simplistix.co.uk/svn/xlwt/trunk/) includes the ability to write
"rich text" to a cell. "Rich text" allows slices of the text to have
their own Font object.

You could do something like this:

from xlrd import open_workbook
from xlwt import easyxf, easyfont
from xlutils.copy import copy
from sys import argv
bold_font = easyfont('bold true') # put whatever else you want in
here
new_xf = easyxf('') # put whatever you want in here
rb = open_workbook(argv[1], formatting_info=True)
rs = rb.sheet_by_index(0)
wb = copy(rb)
ws = wb.get_sheet(0)
for row_index in xrange(rs.nrows):
rvalue = rs.cell(row_index, 1).value
myword = rvalue.split(' ')[0]
rt = [(myword, bold_font), rvalue[len(myword):]]
# the remainder of the string will use new_xf.font
ws.write_rich_text(row_index, 1, rt, new_xf)
wb.save(argv[2])

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?

John Machin

unread,
Jan 24, 2011, 5:25:27 AM1/24/11
to python-excel


On Jan 24, 10:45 am, John Machin <sjmac...@lexicon.net> wrote:
> On Jan 24, 7:49 am, AntonS Sokol <ti.poln...@gmail.com> wrote:
>
[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])

AntonS Sokol

unread,
Jan 24, 2011, 11:30:07 AM1/24/11
to python-excel
This is for my work and I'll be use it every week.
John Machin, sorry, but your code doesn't work for me.
---
Traceback (most recent call last):
File "/private/var/root/Documents/workspace/xslout/src/main.py",
line 10, in <module>
wb, wxfs = xlutils.copy.copy2(rb)
AttributeError: 'module' object has no attribute 'copy2'
---
And if I change "copy2" to "copy" then I get error:
----
Traceback (most recent call last):
File "/private/var/root/Documents/workspace/xslout/src/main.py",
line 10, in <module>
wb, wxfs = xlutils.copy.copy(rb)
TypeError: 'Workbook' object is not iterable

How I can fix this error?

AntonS Sokol

unread,
Jan 24, 2011, 11:51:33 AM1/24/11
to python-excel
Now I fix it, but get error:
from xlwt.Style import easyfont
ImportError: cannot import name easyfont
from xlwt import easyfont - this also not work…
Version xlwt 0.7.3a

John Machin

unread,
Jan 24, 2011, 3:01:30 PM1/24/11
to python-excel
The second lump of code that I gave you doesn't import easyfont and
doesn't need to import easyfont, or anything else from xlwt, nor xlwt
itself.

However you should be able to import easyfont.

(1) Ensure that you have obtained the latest revision from the svn
repo. The last 3 non-blank lines in __init__.py should be

from Formatting import Font, Alignment, Borders, Pattern, Protection,
RichText
from Style import XFStyle, easyxf, easyfont
from ExcelFormula import *

Note carefully the "RichText" and "easyfont".

(2) Please consider the possibility that you don't have xlwt 0.7.3a in
a directory that's in sys.path. What happens when you do the
following?

| C:\Users\John>\python23\python
| Python 2.3.5 (#62, Feb 8 2005, 16:23:02) [MSC v.1200 32 bit
(Intel)] on win32
| Type "help", "copyright", "credits" or "license" for more
information.
| >>> import xlwt
| >>> xlwt.__VERSION__
| '0.7.3a'
| >>> xlwt.__file__
| 'C:\\python23\\lib\\site-packages\\xlwt\\__init__.pyc'
| >>> xlwt.easyfont
| <function easyfont at 0x018BC6B0>
| >>> ^Z

(3) If you are still having problems, please show the full traceback
and error message, and show what ccode of yours has actually executed
up to that point.

AntonS Sokol

unread,
Jan 25, 2011, 7:47:28 AM1/25/11
to python-excel
I uninstall python 2.7 and set to default 2.6 and all is working.Thank
you very much!!You did a great job for me!
Reply all
Reply to author
Forward
0 new messages