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.