AttributeError: 'NoneType' object has no attribute 'groupdict' error while Loading a Excel file

1,155 views
Skip to first unread message

Guruswamy B M

unread,
Mar 11, 2015, 3:18:37 AM3/11/15
to openpyx...@googlegroups.com
Hi,

I am trying to read existing excel file to update some values. But OpenPyExcel giving "AttributeError: 'NoneType' object has no attribute 'groupdict'" error on loading excel.

Excel sheet has formulated in some columns. Tried below code.


>>> import openpyxl
>>> from openpyxl import load_workbook
>>> wb2 = load_workbook('D:\Aegis_POC\Agies_GUI\e.xlsx')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python27\lib\site-packages\openpyxl\reader\excel.py", line 154, in load_workbook
    _load_workbook(wb, archive, filename, read_only, keep_vba)
  File "C:\Python27\lib\site-packages\openpyxl\reader\excel.py", line 242, in _load_workbook
    keep_vba=keep_vba)
  File "C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py", line 323, in read_worksheet
    fast_parse(ws, xml_source, shared_strings, style_table, color_index)
  File "C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py", line 311, in fast_parse
    parser.parse()
  File "C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py", line 92, in parse
    dispatcher[tag_name](element)
  File "C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py", line 206, in parse_header_footer
    self.ws.header_footer.setHeader(oddHeader.text)
  File "C:\Python27\lib\site-packages\openpyxl\worksheet\header_footer.py", line 169, in setHeader
    matches = _split_string(item)
  File "C:\Python27\lib\site-packages\openpyxl\worksheet\header_footer.py", line 202, in _split_string
    return m.groupdict()
AttributeError: 'NoneType' object has no attribute 'groupdict'

However, I have tried with "Use_iterator = True", in this case Load_workbook is successful, but after some update and saving entire workbook become blank (Existing content wiped out and new values also not present in excel). Code below:

>>> wb2 = load_workbook('D:\sample\e.xlsx', use_iterators=True)
>>> t = wb2.get_sheet_by_name(name="Malls")
>>> t.cell(row=1, column=1,).value="1111111"
>>> wb2.save('D:\sample\e.xlsx')

In the above code, updating value "1111111" in row 1 and column1 in "Malls" worksheet. But after step 4 excel sheet "e.xlsx' complet blank. Only Sheets are present.

Any help on this issue, highly appreciated. Thanks

Charlie Clark

unread,
Mar 11, 2015, 3:57:58 AM3/11/15
to openpyx...@googlegroups.com
Hi,

Am .03.2015, 08:18 Uhr, schrieb Guruswamy B M <bm.gur...@gmail.com>:

> Hi,
> I am trying to read existing excel file to update some values. But
> OpenPyExcel giving *"AttributeError: 'NoneType' object has no attribute
> 'groupdict'" *error on loading excel.

FWIW the libarary is called openpyxl.
The is related to a header that openpyxl can't read. To resolve this we'll
need to look at the file. What version of openpyxl are you using?

> However, I have tried with "Use_iterator = True", in this case
> Load_workbook is successful, but after some update and saving entire
> workbook become blank (Existing content wiped out and new values also not
> present in excel).

use_iterators=True is the same as read_only=True: the file cannot be saved.

Code below:
>
>>>> wb2 = load_workbook('D:\sample\e.xlsx', use_iterators=True)
>>>> t = wb2.get_sheet_by_name(name="Malls")
>>>> t.cell(row=1, column=1,).value="1111111"
>>>> wb2.save('D:\sample\e.xlsx')
> In the above code, updating value "1111111" in row 1 and column1 in
> "Malls"
> worksheet. But after step 4 excel sheet "e.xlsx' complet blank. Only
> Sheets
> are present.

You should get an exception telling you the file is read-only.

> Any help on this issue, highly appreciated. Thanks

A bug report with a sample file, please.

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

Guruswamy B M

unread,
Mar 11, 2015, 4:47:55 AM3/11/15
to openpyx...@googlegroups.com
Hi Charlie,

Thanks for the response.

I have attached sample file which is unable to load the file in OpenPyExcel.

Installed openpyxl version is '2.2.0-b1'

Also I didnt get any exception error on using "user_iterator = True", as you mentioned. Please have a look and suggest me resolve this issue.

Thanks,
Guru
sample.xlsx

Charlie Clark

unread,
Mar 11, 2015, 5:33:54 AM3/11/15
to openpyx...@googlegroups.com
Am .03.2015, 09:47 Uhr, schrieb Guruswamy B M <bm.gur...@gmail.com>:

> Hi Charlie,
> Thanks for the response.
> I have attached sample file which is unable to load the file in
> OpenPyExcel.

> Installed openpyxl version is *'2.2.0-b1'*

Any reason why you installed this particular version? I think it's good
that the beta is getting some testing.

I'm almost certain the problem is related to the images embedded in the
page footer. In the code we could work around this by issuing a warning
rather if the header or footer can't be parsed. Or you can remove them
from the file. Unfortunately, the mini-specification for headers and
footers is not easy to implement in its entirety because it allows
nesting. 2.2 contains vastly improved support for this but that's a far as
it's going to get unless someone else implements the specification with a
nice API.

> Also I didnt get any exception error on using "user_iterator = True", as
> you mentioned. Please have a look and suggest me resolve this issue.

I've created a bug for this:
https://bitbucket.org/openpyxl/openpyxl/issue/429

Charlie Clark

unread,
Mar 11, 2015, 5:55:21 AM3/11/15
to openpyx...@googlegroups.com
Am .03.2015, 10:33 Uhr, schrieb Charlie Clark
<charli...@clark-consulting.eu>:

> Also I didnt get any exception error on using "user_iterator = True", as
> you mentioned. Please have a look and suggest me resolve this issue.

You can use the following code in worksheet/header_footer.py:

from warnings import warn

def _split_string(text):
"""Split the combined (decoded) string into left, center and right
parts"""
m = ITEM_REGEX.match(text)
try:
parts = m.groupdict()
except AttributeError:
warn("""Cannot parse header or footer so it will be ignored""")
return {'left':'', 'right':'', 'center':''}

Charlie Clark

unread,
Mar 11, 2015, 3:34:00 PM3/11/15
to openpyx...@googlegroups.com
Am .03.2015, 10:55 Uhr, schrieb Charlie Clark
<charli...@clark-consulting.eu>:

> You can use the following code in worksheet/header_footer.py:

It turns out the problem was largely the multiline footer. This is now
fixed, though the logos are removed. However, the fix didn't make it into
2.2 which was released this afternoon. You can either use a checkout of
the 2.2 branch or pin your environment to the 2.1.5 release (this doesn't
preserve the footer at all) and wait for the next 2.2 patch release.

Guruswamy B M

unread,
Mar 12, 2015, 2:10:25 AM3/12/15
to openpyx...@googlegroups.com
Thanks Charlie.

Noted down. Now I have removed header and footer from the file and able to load and update the file successfully.

I will wait for release 2.2.

Regards,
Guru

On Thu, Mar 12, 2015 at 1:03 AM, Charlie Clark <charli...@clark-consulting.eu> wrote:
Am .03.2015, 10:55 Uhr, schrieb Charlie Clark <charlie.clark@clark-consulting.eu>:

You can use the following code in worksheet/header_footer.py:

It turns out the problem was largely the multiline footer. This is now fixed, though the logos are removed. However, the fix didn't make it into 2.2 which was released this afternoon. You can either use a checkout of the 2.2 branch or pin your environment to the 2.1.5 release (this doesn't preserve the footer at all) and wait for the next 2.2 patch release.


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

--
You received this message because you are subscribed to a topic in the Google Groups "openpyxl-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openpyxl-users/4ZFUilkhFHU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openpyxl-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Charlie Clark

unread,
Mar 12, 2015, 3:26:03 AM3/12/15
to openpyx...@googlegroups.com
Am .03.2015, 07:10 Uhr, schrieb Guruswamy B M <bm.gur...@gmail.com>:

> Thanks Charlie.

> Noted down. Now I have removed header and footer from the file and able
> to load and update the file successfully.

Good.

> I will wait for release 2.2.

2.2 has been released, you're going to have to wait for 2.2.1
Reply all
Reply to author
Forward
0 new messages