Load_workbook not working

1,760 views
Skip to first unread message

nicholaic...@gmail.com

unread,
Jun 5, 2013, 6:27:58 PM6/5/13
to openpyx...@googlegroups.com
I have been using openpyxl just fine till today when I tried to run my simple script and the console game me this error: AttributeError: 'NoneType' object has no attribute 'replace'.  This error was given when I called load_workbook it gave a trace back and on line 260 in setHeader in worksheet.py (in the module) it is trying to run the code itemArray = [i.replace('#DOUBLEAMP#', '&&') for i in item.replace('&&', '#DOUBLEAMP#').split('&')].

Just a simple
import openpyxl
workbook_read = openpyxl.load_workbook('PATH/NAME/file.xlsx')
will give me this error.

Has anyone else had this problem?  Or any suggestions as how to fix it?

Charlie Clark

unread,
Jun 6, 2013, 3:41:08 AM6/6/13
to openpyx...@googlegroups.com
Hi Nicholai,
Can you perhaps some information on the cells in your sheet? It sounds
like a bug so you might want to submit an issue with a sample workbook.

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

nicholaic...@gmail.com

unread,
Jun 6, 2013, 2:09:49 PM6/6/13
to openpyx...@googlegroups.com
Yeah it a pretty basic text file with just numbers and letters and no more then 300 rows long.  Attached is the file I am trying to read in.  I've tried uninstalling and reinstalling openpyxl with no change.
add shelf life working copy.xlsx

Alejandro Martinis

unread,
Jun 6, 2013, 3:22:41 PM6/6/13
to openpyx...@googlegroups.com, nicholaic...@gmail.com
Hello all.

Yes, I had the same problem today after upgrading to 1.62

All I could do was this crude hack:

    def setHeader(self, item):
       try:
                itemArray = [i.replace('#DOUBLEAMP#', '&&') for i in item.replace('&&', '#DOUBLEAMP#').split('&')]
                l = itemArray.index('L') if 'L' in itemArray else None
                c = itemArray.index('C') if 'C' in itemArray else None
                r = itemArray.index('R') if 'R' in itemArray else None
                if l:
                   if c:
                        self.left_header.set(itemArray[l:c])
                   elif r:
                        self.left_header.set(itemArray[l:r])
                   else:
                        self.left_header.set(itemArray[l:])
                if c:
                   if r:
                        self.center_header.set(itemArray[c:r])
                   else:
                        self.center_header.set(itemArray[c:])
                if r:
                   self.right_header.set(itemArray[r:])
       except:
          pass


... to wrap the code around a try block.

I was wondering about that  " i " variable there. It holds a None value and I could not trace it coming from anywhere before.

Actually I wasn't even going to mention this since I was blaming myself ( I upgraded from a previous version and thought maybe I had done something wrong) so I'm glad Nicholai raises the issue here and gives me courage to post. :)

Thanks to all

Alejandro.

Charlie Clark

unread,
Jun 6, 2013, 3:27:31 PM6/6/13
to openpyx...@googlegroups.com
Thanks,

I'll try and have a look at this over the weekend as it sounds like a bug.

Charlie Clark

unread,
Jun 9, 2013, 9:21:40 AM6/9/13
to openpyx...@googlegroups.com
Am 06.06.2013, 21:27 Uhr, schrieb Charlie Clark
<charli...@clark-consulting.eu>:

> I'll try and have a look at this over the weekend as it sounds like a
> bug.

Yes, this is a bug. It looks like openpyxl cannot handle some empty tags.

From the source:

<headerFooter differentFirst="false" differentOddEven="false">
<oddHeader/>
<oddFooter/>
</headerFooter>


The relevant code in reader/worksheet.py

https://bitbucket.org/ericgazoni/openpyxl/src/bbf6f57f094eab319d57c009e4ac21fb9fad5815/openpyxl/reader/worksheet.py?at=default#cl-207

headerFooter = root.find(QName(xmlns, 'headerFooter').text)
if headerFooter is not None:
oddHeader = headerFooter.find(QName(xmlns, 'oddHeader').text)
if oddHeader is not None:
ws.header_footer.setHeader(oddHeader.text)
oddFooter = headerFooter.find(QName(xmlns, 'oddFooter').text)
if oddFooter is not None:
ws.header_footer.setFooter(oddFooter.text)

A quick fix is to adjust the conditions for oddHeader and oddFooter to
refer to their text attributes.

if oddHeader.text is not None:…

A quick check indicates that this does not break any tests, however, I
suspect that is simply because there are no tests for it. fast_parse() is
one of the more monolithic methods for which writing tests is not much
fun. I'll submit a bug on this and see if it can't be refactored a bit to
make it more testable.

Charlie Clark

unread,
Jun 9, 2013, 9:24:07 AM6/9/13
to openpyx...@googlegroups.com
Am 09.06.2013, 15:21 Uhr, schrieb Charlie Clark
<charli...@clark-consulting.eu>:

> I'll submit a bug on this and see if it can't be refactored a bit to
> make it more testable.

hm, it seems there is a similar bug:

https://bitbucket.org/ericgazoni/openpyxl/issue/201/cannot-open-xlsx-that-was-last-saved-using

Nicolai, have you edited or created the file with LibreOffice? Not that
that doesn't mean the bug is valid, just nice not to duplicate bugs.

nicholaic...@gmail.com

unread,
Jun 9, 2013, 1:14:00 PM6/9/13
to openpyx...@googlegroups.com
I do use LibreOffice to edit the files.  This does seem to be the same bug.
Reply all
Reply to author
Forward
0 new messages