Strange xlutils problem

903 views
Skip to first unread message

Beth Cimini

unread,
Dec 11, 2009, 9:15:20 AM12/11/09
to python-excel
Hi all,
I'm a python (and coding) beginner and I'm getting a very weird error
from xlutils. I'm trying to write some software that will check if an
excel file exists,create it if not, and write various things I want to
it. The code I'm using to do that is below.
My problem is that xlutils will work (and therefore the whole program
works fine) until I close the .py file, at which point it suddenly
stops working, my try statement therefore fails regardless and it
creates a new blank document even if there was already good data
underneath. The only thing I can do that seems to fix this is see-
sawing between using "import xlutils" or "from xlutils import copy"-
the mere act of switching seems to be enough to do it, but I need to
switch it every single time I open the file, which is clearly not a
long-term solution. I'm running xlutils 1.4.1 (over xlrd 0.7.1 and
xlwt 0.7.2) in IDLE in python 2.6.2 on a computer running 64-bit
Vista. Is this a known bug, or is there something stupid and obvious
I'm doing wrong? Any suggestions besides uninstalling and
reinstalling, which I've done? Thanks so much!
~Beth

import xlwt
import xlutils
import xlrd

def tofile(inbook, sheetin, outbook, sheetout):
try:
wb=xlrd.open_workbook(outbook,formatting_info=True)
w=xlutils.copy.copy(wb)
except:
w=xlwt.Workbook()
ws=w.add_sheet(sheetout)
ws.write(---various things that the program I call does---)
w.save(outbook)

Chris Withers

unread,
Dec 11, 2009, 9:22:35 AM12/11/09
to python...@googlegroups.com
Beth Cimini wrote:
> Hi all,
> I'm a python (and coding) beginner and I'm getting a very weird error
> from xlutils.

How about telling us what that error is? Full traceback please...

> My problem is that xlutils will work (and therefore the whole program
> works fine) until I close the .py file,

What .py file?

> at which point it suddenly
> stops working, my try statement

Bare try/excepts as you have them are bad. Don't ever do that. Let the
exception propagate and you might stand a chance at finding out what
went wrong.

> long-term solution. I'm running xlutils 1.4.1 (over xlrd 0.7.1 and
> xlwt 0.7.2) in IDLE in python 2.6.2 on a computer running 64-bit
> Vista.

Try just running the script from a command prompt.

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk

Adrian Klaver

unread,
Dec 11, 2009, 9:36:14 AM12/11/09
to python...@googlegroups.com
At least part of the problem is above. If there are no errors then w will be a
copy of wb. If there is any error in using either xlrd or xlutils then w
becomes a new workbook and hence a blank document. Per Chris suggestion you
would be better off letting the error propagate.

> ws=w.add_sheet(sheetout)
> ws.write(---various things that the program I call does---)
> w.save(outbook)
>
> --




--
Adrian Klaver
akl...@comcast.net

Beth Cimini

unread,
Dec 11, 2009, 9:52:04 AM12/11/09
to python-excel
Hi Chris,
Apologies for being unclear- my brain is a little fried this time of
morning and I'm new at decoding these kinds of problems. I usually
get one of two errors (without the try/except statement of course):
If using "from xlutils.copy import copy"
Traceback (most recent call last):
File "C:\Python26\TT3.py", line 24, in <module>
tofile('C:\Python26\trash.xls', 'test1', 'testtrash.xls', '5')
File "C:\Python26\TT3.py", line 18, in tofile
w=xlutils.copy.copy(wb)
NameError: global name 'xlutils' is not defined

OR
If using "import xlutilis"
Traceback (most recent call last):
File "C:\Python26\TT3.py", line 23, in <module>
tofile('C:\Python26\trash.xls', 'test1', 'testtrash.xls', '5')
File "C:\Python26\TT3.py", line 18, in tofile
w=xlutils.copy.copy(wb)
AttributeError: 'module' object has no attribute 'copy'

The py file I was referring to was the IDLE file containing the
module- while it's open I can run it successfully in the shell several
times, but as soon as I shut it and reopen it I get one of the above
two errors. I seem to be getting them both intermittently in the
command line as well.

Chris Withers

unread,
Dec 11, 2009, 10:12:03 AM12/11/09
to python...@googlegroups.com
Beth Cimini wrote:
> Hi Chris,
> Apologies for being unclear- my brain is a little fried this time of
> morning and I'm new at decoding these kinds of problems. I usually
> get one of two errors (without the try/except statement of course):
> If using "from xlutils.copy import copy"
> Traceback (most recent call last):
> File "C:\Python26\TT3.py", line 24, in <module>
> tofile('C:\Python26\trash.xls', 'test1', 'testtrash.xls', '5')
> File "C:\Python26\TT3.py", line 18, in tofile
> w=xlutils.copy.copy(wb)
> NameError: global name 'xlutils' is not defined

This indicates you didn't import xlutils...

> OR
> If using "import xlutilis"
> Traceback (most recent call last):
> File "C:\Python26\TT3.py", line 23, in <module>
> tofile('C:\Python26\trash.xls', 'test1', 'testtrash.xls', '5')
> File "C:\Python26\TT3.py", line 18, in tofile
> w=xlutils.copy.copy(wb)
> AttributeError: 'module' object has no attribute 'copy'

...and my guess here is that you've imported xlutils, but not copy:

>>> import xlutils
>>> xlutils.copy.copy
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'module' object has no attribute 'copy'

It's much better for to use:

from xlutils.copy import copy

> The py file I was referring to was the IDLE file containing the
> module- while it's open I can run it successfully in the shell several
> times, but as soon as I shut it and reopen it I get one of the above
> two errors. I seem to be getting them both intermittently in the
> command line as well.

It sounds like you're having trouble using IDLE as a text editor.
I just you try doing this in a command prompt window:

C:\Python26> type TT3.py
C:\Python26> python TT3.py

...then you'll see what code you actually have on disk.

Please remove that try/except. It's a stupid idea...

John Machin

unread,
Dec 11, 2009, 3:34:27 PM12/11/09
to python...@googlegroups.com
On 12/12/2009 1:52 AM, Beth Cimini wrote:

> I usually
> get one of two errors (without the try/except statement of course):
> If using "from xlutils.copy import copy"
> Traceback (most recent call last):
> File "C:\Python26\TT3.py", line 24, in <module>
> tofile('C:\Python26\trash.xls', 'test1', 'testtrash.xls', '5')

Nothing to do with your current problem, but the above statement shows
symptoms of 3 or 4 bad habits which will bite you one day if you don't
cure yourself:

(1) Keep your own data files etc well away from the directories in which
third-party software is installed. Put them in directories with
meaningful names. Consider what you will do when you upgrade to Python 2.7.

(2) Try to avoid hard-coding file paths in your scripts. If you must do
it, you need to bear in mind that when using Python on Windows, the
backslash character is overloaded; it's both a path separator
(C:\foo\bar.zot) and an escape character used to represent control
characters etc (\r\n\t). By the time that Windows gets the first
argument, that \t will be a TAB ('\x09'), not a backslash followed by a
't' ... consquently, the path will be invalid.

There are 3 ways around this (apart from not hard-coding at all):

| >>> a = 'C:\Python26\trash.xls'
| >>> b = r'C:\Python26\trash.xls'
| >>> c = 'C:\\Python26\\trash.xls'
| >>> d = 'C:/Python26/trash.xls'
| >>> map(len, [a,b,c,d])
| [20, 21, 21, 21]
| >>> a == b
| False
| >>> b == c
| True
| >>> open(a)
| Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
| IOError: [Errno 22] invalid mode ('r') or filename:
'C:\\Python26\trash.xls'
| >>> open(b)
| Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
| IOError: [Errno 2] No such file or directory: 'C:\\Python26\\trash.xls'
| >>> open(c)
| Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
| IOError: [Errno 2] No such file or directory: 'C:\\Python26\\trash.xls'
| >>> open(d)
| Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
| IOError: [Errno 2] No such file or directory: 'C:/Python26/trash.xls'
| >>>

(3) The script snippet you posted first didn't use the first (inbook)
arg to that function
and/or
(4) You may not be posting that you actually ran.


> File "C:\Python26\TT3.py", line 18, in tofile
> w=xlutils.copy.copy(wb)
> NameError: global name 'xlutils' is not defined

If as you say you have done
from xlutils.copy import copy
then you should call it by doing
w = copy(wb)

HTH,
John

Beth Cimini

unread,
Dec 11, 2009, 6:57:52 PM12/11/09
to python-excel
Thank you all for the help and input. I was able to get the
particular problem I posted about by using both "import xlutils" and
"from xlutils.copy import copy" together at the beginning of my
script. I know I definitely still have lots to learn about the right
way to do things so thanks for the insights.
~Beth

cristian rosa

unread,
Jul 3, 2013, 4:02:28 PM7/3/13
to python...@googlegroups.com, beth....@gmail.com
Did you find any solution for this? I´ll be researching a long time ago and seems 
that nobody fixed it, Im working with Sikuli trying to read a file get some values 
from that file write some info in it and save it. Imposible so far...
Thanks!

Adrian Klaver

unread,
Jul 5, 2013, 10:16:56 AM7/5/13
to python...@googlegroups.com, cristian rosa, beth....@gmail.com
On 07/03/2013 01:02 PM, cristian rosa wrote:
> Did you find any solution for this? I�ll be researching a long time ago
> and seems
> that nobody fixed it, Im working with Sikuli trying to read a file get
> some values
> from that file write some info in it and save it. Imposible so far...
> Thanks!
>
> El viernes, 11 de diciembre de 2009 11:15:20 UTC-3, Beth Cimini escribi�:
>
> Hi all,
> I'm a python (and coding) beginner and I'm getting a very weird error
> from xlutils. I'm trying to write some software that will check if an
> excel file exists,create it if not, and write various things I want to
> it. The code I'm using to do that is below.
> My problem is that xlutils will work (and therefore the whole program
> works fine) until I close the .py file, at which point it suddenly
> stops working,my try statement therefore fails regardless and it
> creates a new blank document even if there was already good data
> underneath.

The above is the part I do not understand. How are you closing the file?
Not sure how that would affect the try statement, since you would be
past that at that point.


> The only thing I can do that seems to fix this is see-
> sawing between using "import xlutils" or "from xlutils import copy"-
> the mere act of switching seems to be enough to do it, but I need to
> switch it every single time I open the file, which is clearly not a
> long-term solution. I'm running xlutils 1.4.1 (over xlrd 0.7.1 and
> xlwt 0.7.2) in IDLE in python 2.6.2 on a computer running 64-bit
> Vista. Is this a known bug, or is there something stupid and obvious
> I'm doing wrong? Any suggestions besides uninstalling and
> reinstalling, which I've done? Thanks so much!
> ~Beth
>
> import xlwt
> import xlutils
> import xlrd
>
> def tofile(inbook, sheetin, outbook, sheetout):
> try:
> wb=xlrd.open_workbook(outbook,formatting_info=True)
> w=xlutils.copy.copy(wb)
> except:
> w=xlwt.Workbook()
> ws=w.add_sheet(sheetout)
> ws.write(---various things that the program I call does---)
> w.save(outbook)
>
>


--
Adrian Klaver
adrian...@gmail.com

Chris Withers

unread,
Jul 5, 2013, 1:04:59 PM7/5/13
to python...@googlegroups.com, cristian rosa, beth....@gmail.com
On 03/07/2013 21:02, cristian rosa wrote:
> def tofile(inbook, sheetin, outbook, sheetout):
> try:
> wb=xlrd.open_workbook(outbook,formatting_info=True)
> w=xlutils.copy.copy(wb)
> except:
> w=xlwt.Workbook()

Bare try/excepts are evil and stupid. Don't do that.

cheers,
Reply all
Reply to author
Forward
0 new messages