Re: Problem with get_sheet xlutils

397 views
Skip to first unread message

John Machin

unread,
Jul 8, 2012, 1:23:20 PM7/8/12
to python-excel


On Jul 8, 7:13 pm, resta <thibaut.fourc...@gmail.com> wrote:
> Hello,
> I'm a beginner with python and i have a problem when modifying an excel
> file. I'm working on python 2.7 on a windows machine. Here is the code I
> use:
>
> global wbook

Why?

> import sys
> sys.path.append('C:\\Python27\\Lib\\site-packages');

Firstly, lose the semicolon. This is Python, not C(++|#|)
Secondly, lose the remainder of the line -- it's quite unnecessary.

> import fpformat, os, random, time
> import xlrd, xlwt, xlutils

Given the next few lines, what do you have the above line?

> from xlrd import open_workbook
> from xlwt import easyxf
> print xlrd.__VERSION__, xlrd.__file__
> print xlwt.__VERSION__, xlwt.__file__
> print xlutils.__VERSION__, xlutils.__file__
> from xlutils.copy import copy

'copy' is a very common function/method name. Better:

from xlutils.copy import copy as xlutils_copy

and later call xlutils_copy.

> import numpy as np

Given the next line, what is the point of the above line?

> from numpy import *

Dangerous. Is there a numpy.copy?

Yes, see http://docs.scipy.org/doc/numpy/reference/generated/numpy.copy.html
... and, for future reference, it returns an numpy.ndarray object.

> import scipy.optimize

Given the next line, ... etc etc
> from scipy import *

Dangerous. Is there a scipy.copy? Is it the same as numpy.copy?

>
> # # Corps du programme
> Fichier='120210-Ti-300N-test.xls'
> rbook=open_workbook(Fichier)
> rs=rbook.sheet_by_index(0)
> # rbook=open_workbook(Fichier,logfile=sys.stdout, verbosity=0,
> pickleable=True, use_mmap=USE_MMAP, file_contents=None,
> encoding_override=None, formatting_info=False, on_demand=False, )
> wbook=copy(Fichier)

Fichier represents the string '120210-Ti-300N-test.xls'.
xlutils.copy.copy expects an xlrd.Book object (for example, rbook),
NOT a string. That's problem 1.

Problem 2 is that you are using the wrong copy. The error message says
wbook represents an numpy.ndarray object. It seems that copy is
numpy.copy.

To show this, insert the following lines here and see what is printed:

print "The copy being used is", repr(copy)
print "wbook refers to", repr(wbook)

For educational purposes, try fixing only Problem 1:

wbook = copy(rbook)

This should give an immediate error -- numpy.copy is not likely to
find an xlrd.Book object very palatable.

Fixing both problems: use wbook = xlutils_copy(rbook)

> sheet=wbook.get_sheet(0)
>
> Everything go well until the wbook.get_sheet(0).
> Here i have this error message:
> AttributeError: 'numpy.ndarray' object has no attribute 'get_sheet'
>
> I've read elsewhere that could come from too old versions of xlrd and xlwt,

Where did you read that??? It's absolutely nothing to do with whether
old versions are in use or not!!! It's quite simple: You used
numpy.copy instead of xlutils.copy.copy.

John Machin

unread,
Jul 8, 2012, 1:34:55 PM7/8/12
to python-excel


On Jul 9, 3:23 am, John Machin <sjmac...@lexicon.net> wrote:

> print "The copy being used is", repr(copy)
and should have written;

print "The copy being used is from", copy.__module__

Andrea Gavana

unread,
Jul 8, 2012, 2:34:21 PM7/8/12
to python...@googlegroups.com
Hi,

On 8 July 2012 11:13, resta wrote:
> Hello,
> I'm a beginner with python and i have a problem when modifying an excel
> file. I'm working on python 2.7 on a windows machine. Here is the code I
> use:
>
> global wbook
> import sys
> sys.path.append('C:\\Python27\\Lib\\site-packages');
> import fpformat, os, random, time
> import xlrd, xlwt, xlutils
> from xlrd import open_workbook
> from xlwt import easyxf
> print xlrd.__VERSION__, xlrd.__file__
> print xlwt.__VERSION__, xlwt.__file__
> print xlutils.__VERSION__, xlutils.__file__
> from xlutils.copy import copy
> import numpy as np
> from numpy import *
> import scipy.optimize
> from scipy import *
>
> # # Corps du programme
> Fichier='120210-Ti-300N-test.xls'
> rbook=open_workbook(Fichier)
> rs=rbook.sheet_by_index(0)
> # rbook=open_workbook(Fichier,logfile=sys.stdout, verbosity=0,
> pickleable=True, use_mmap=USE_MMAP, file_contents=None,
> encoding_override=None, formatting_info=False, on_demand=False, )
> wbook=copy(Fichier)
>
> # for sheet in wbook.sheets():
> # if sheet.name[0]=='t' or sheet.name[0]=='T':
> # print sheet.col_values(1,19,102)
> sheet=wbook.get_sheet(0)
>
> Everything go well until the wbook.get_sheet(0).
> Here i have this error message:
> AttributeError: 'numpy.ndarray' object has no attribute 'get_sheet'
>
> I've read elsewhere that could come from too old versions of xlrd and xlwt,
> so i checked and I use the latest versions.
>
> If somebody has an idea where the problem come from and how it can be fixed,
> it would help me a lot.
> I thank you all in advance for the time you'll spend.

You will have to read some of the Python tutorials, and especially the
ones focusing on why it is bad, dangerous and not recommended to do
something like:

from module import *

Don't do that, *especially* with numpy and scipy. From what I can see
from your code (and I didn't run it, I am scared of blowing up my
computer), you are messing way too much with the namespace for your
script to give any meaningful results. Drop the full import.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

# ------------------------------------------------------------- #
def ask_mailing_list_support(email):

if mention_platform_and_version() and include_sample_app():
send_message(email)
else:
install_malware()
erase_hard_drives()
# ------------------------------------------------------------- #

resta

unread,
Jul 8, 2012, 4:30:13 PM7/8/12
to python...@googlegroups.com
Thank you all for your answer. My first error was Problem 2 and i add problem 1 when I tried to fix it. I changed the both and it works now.
Thanks to all your answers I realized that I wasn't aware of all the thing i do wrong. Eventually, i learned that i still have a lot of thing to learn to write a proper code with Python.
Reply all
Reply to author
Forward
0 new messages