Saving an existing file

2,959 views
Skip to first unread message

neocortex

unread,
Feb 25, 2011, 5:30:24 AM2/25/11
to openpyxl-users
Hello!
It is a bit in shade how to save an existing file where you did some
changes, like putting values in cells, or adding new sheet etc. Can
anyone explain me how can I do that?

This seems not to work:
>>> from openpyxl.writer.excel import save_workbook
>>> save_workbook(wb, 'test.xlsx')

Is this now in ExcelWriter? save_workbook() seems not working?

Best,
PM

Eric Gazoni

unread,
Feb 25, 2011, 7:15:13 AM2/25/11
to openpyx...@googlegroups.com
Hi,
you can ignore the ExcelWriter, and just call wb.save(filename) instead,
it should work.

Cheers,
Eric

Le 25/02/11 11:30, neocortex a �crit :

Message has been deleted

neocortex

unread,
Mar 1, 2011, 7:10:20 AM3/1/11
to openpyxl-users
Nothing seems to work! I tried "old-way" (documented at:
http://packages.python.org/openpyxl/tutorial.html#saving-to-a-file),
and a "new-way" (documented at: http://packages.python.org/openpyxl/usage.html?highlight=excelwriter).
Also, I tried what you
have suggested above [ wb.save(filename) ], but, again: error.
I wrote following lines (only those relevant for openpyxl):

from openpyxl.writer.excel import ExcelWriter
from openpyxl.reader.excel import load_workbook
from openpyxl.cell import get_column_letter

################################################################################

# Gets number of relevant rows and columns
# (calculate_dimension() is not working as well, so I did it by
muself):
def dimensions(sh):
col = list(); col_idx = 1; row_idx = 1
while sh.cell(get_column_letter(col_idx)+'1').value != None:
col.append(get_column_letter(col_idx))
col_idx += 1
while sh.cell('A'+str(row_idx)).value != None:
row_idx += 1
return [row_idx, col_idx, col]

################################################################################

filename = r'bigrams'
wb = load_workbook(filename + '.xlsx')
ws = wb.worksheets[0]
dims = dimensions(ws)

# ......

wsNew1 = wb.create_sheet()
wsNew1.title = 'rawGT'

ew = ExcelWriter(wb)
ew.save(filename + '_NEW.xlsx')

From this, I am getting error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/pymodules/python2.6/openpyxl/writer/excel.py", line
51, in save
content = write_string_table(string_table = shared_string_table))
File "/usr/lib/pymodules/python2.6/openpyxl/writer/strings.py", line
57, in write_string_table
if key.strip() != key:
AttributeError: 'NoneType' object has no attribute 'strip'

What I am doing wrong?! Please, HELP ME!!!
I was so happy when I have found openpyxl, and now I am stuck...

Best,
Petar

Eric

unread,
Mar 1, 2011, 7:23:08 AM3/1/11
to openpyx...@googlegroups.com, pmi...@gmail.com
Hi,
it sounds like a bug we had in a previous version. Are you using the latest one (tip from bitbucket) ?

Can  you do:
import openpyxl
print openpyxl.__version__
and give me the output of this ?

Next, the current proper way of saving a file is :

wb = load_workbook('filename.xlsx')
wb.save('newfilename.xlsx')

But make sure you're using the latest version.

If none of this works, then please fill a bug report (with your script and sample data attached) on the bitbucket bug tracker and someone (probably me) will have a look at it ASAP.

Cheers,
Eric



2011/3/1 neocortex <pmi...@gmail.com>

Petar Milin

unread,
Mar 1, 2011, 7:41:43 AM3/1/11
to Eric, openpyx...@googlegroups.com, pmi...@gmail.com
Hello! Thanks so much for the prompt answer and help!


On 01/03/11 13:23, Eric wrote:
Hi,
it sounds like a bug we had in a previous version. Are you using the latest one (tip from bitbucket) ?
It could be. I am using Debian testing and I simply got it with the Aptitude.

Can  you do:
import openpyxl
print openpyxl.__version__
and give me the output of this ?
1.1.0

Next, the current proper way of saving a file is :

wb = load_workbook('filename.xlsx')
wb.save('newfilename.xlsx')
And, what about preamble? What and how to use import and from (...) import?

Best,
PM

Eric

unread,
Mar 1, 2011, 7:46:21 AM3/1/11
to pmi...@ff.uns.ac.rs, openpyx...@googlegroups.com
Damn, 1.1.0 was released somehow 1 year ago ! :)
Documentation came out much later, no wonder you can't use openpyxl properly with that :)

Try to get it from bitbucket instead (https://bitbucket.org/ericgazoni/openpyxl/overview).

The imports are like described in the doc though: 
from openpyxl.reader.excel import load_workbook

Cheers,
Eric

2011/3/1 Petar Milin <pmi...@ff.uns.ac.rs>

Petar Milin

unread,
Mar 1, 2011, 8:27:34 AM3/1/11
to Eric, openpyx...@googlegroups.com

On 01/03/11 13:46, Eric wrote:
> Damn, 1.1.0 was released somehow 1 year ago ! :)
> Documentation came out much later, no wonder you can't use openpyxl
> properly with that :)

Sorry! I had such a confidence for Debian!


> Try to get it from bitbucket instead
> (https://bitbucket.org/ericgazoni/openpyxl/overview).

I looked that, but installing via ActivePython seems easier, and it
should give the same results.


> The imports are like described in the doc though:
> from openpyxl.reader.excel import load_workbook

And for writing:
from openpyxl.writer.excel import save_workbook

Best,
Petar

Eric

unread,
Mar 1, 2011, 8:31:41 AM3/1/11
to pmi...@ff.uns.ac.rs, openpyx...@googlegroups.com
no no, for writing:
once you have your workbook object, you just call it's save() method.
There is no external function to save a workbook.

I've never tried ActivePython so I can't promise it will work out of the box as it should with the bitbucket version. I only support this source, but you might have it working better than version 1.0 anyway...

Cheers,
Eric

2011/3/1 Petar Milin <pmi...@ff.uns.ac.rs>


Petar Milin

unread,
Mar 1, 2011, 8:43:45 AM3/1/11
to Eric, openpyx...@googlegroups.com
Then, my last: How to install original source on Linux?
python setup.py [ANYTHING ELSE]

Best,
Petar
-- 
#####################################################################################
Petar Milin
Department of Psychology, University of Novi Sad, Serbia
 &
Laboratory for Experimental Psychology, University of Belgrade, Serbia

Address:   Dr Zorana Dindica 2, Novi Sad 21000, Serbia
E-mail:    pmilin <AT> ff.uns.ac.rs
Tel.&Fax:  +381 21 458 948

Official homepage (in Serbian):
http://www.ff.uns.ac.rs/fakultet/ljudi/fakultet_odseci_psihologija_petar_milin.html

Personal homepage:
http://sites.google.com/site/pmilin/
#####################################################################################


Eric

unread,
Mar 1, 2011, 8:44:32 AM3/1/11
to openpyx...@googlegroups.com
python setup.py install ;-)

2011/3/1 Petar Milin <pmi...@ff.uns.ac.rs>

neocortex

unread,
Mar 1, 2011, 10:49:20 AM3/1/11
to openpyxl-users

python setup.py install

Traceback (most recent call last):
File "setup.py", line 22, in <module>
from setuptools import setup, Extension, find_packages
ImportError: No module named setuptools

:-(

Petar

Eric

unread,
Mar 1, 2011, 11:04:42 AM3/1/11
to pmi...@ff.uns.ac.rs, openpyx...@googlegroups.com
http://stackoverflow.com/questions/2211335/python-setuptools-import-error-using-netbeans

Eric

2011/3/1 Petar Milin <pmi...@ff.uns.ac.rs>
python setup.py install


Traceback (most recent call last):
Reply all
Reply to author
Forward
0 new messages