Skip to content

Python3: No module named 'openpyxl.style' #9060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
upthewazoo opened this issue Dec 12, 2014 · 6 comments
Closed

Python3: No module named 'openpyxl.style' #9060

upthewazoo opened this issue Dec 12, 2014 · 6 comments
Labels
Compat pandas objects compatability with Numpy or Python functions IO Excel read_excel, to_excel

Comments

@upthewazoo
Copy link

There is no module named openpyxl.style on the Python3 version of openpyxl. There is, however a openpyxl.styles module in the namespace of openpyxl. But a simply name change update on line 577 of pandas/io/excel.py isn't the quick fix as there are other differences causing errors.

My offending code:
diff_output = diff_panel.apply(report_diff, axis=0)

def has_change(row):
if "--->" in row.to_string():
return "Y"
else:
return "N"

diff_output['has_change'] = diff_output.apply(has_change, axis=1)

-->diff_output[(diff_output.has_change == 'Y')].to_excel('my-diff.xlsx')

Stacktrace tail

.
.
.
/usr/lib/python3/dist-packages/pandas/io/excel.py in _convert_to_style(cls, style_dict)
575 """
576
--> 577 from openpyxl.style import Style
578 xls_style = Style()
579 for key, value in style_dict.items():

ImportError: No module named 'openpyxl.style'

Name: openpyxl
Version: 2.1.3
Location: /usr/local/lib/python3.4/dist-packages
Requires: jdcal

Name: jdcal
Version: 1.0
Location: /usr/local/lib/python3.4/dist-packages
Requires:

Python 3.4.0 (default, Apr 11 2014, 13:05:11)
Type "copyright", "credits" or "license" for more information.

IPython 1.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: import pandas

In [2]: pandas.__version__
Out[2]: '0.13.1'

Modified 577

(-)from openpyxl.style import Style
(+)from openpyxl.styles import Styles

New Stacktrace


TypeError Traceback (most recent call last)
in ()
----> 1 diff_output[(diff_output.has_change == 'Y')].to_excel('my-diff.xlsx')

/usr/lib/python3/dist-packages/pandas/core/frame.py in to_excel(self, excel_writer, sheet_name, na_rep, float_format, cols, header, index, index_label, startrow, startcol, engine, merge_cells)
1202 formatted_cells = formatter.get_formatted_cells()
1203 excel_writer.write_cells(formatted_cells, sheet_name,
-> 1204 startrow=startrow, startcol=startcol)
1205 if need_save:
1206 excel_writer.save()

/usr/lib/python3/dist-packages/pandas/io/excel.py in write_cells(self, cells, sheet_name, startrow, startcol)
528 style = None
529 if cell.style:
--> 530 style = self._convert_to_style(cell.style)
531 for field in style.fields:
532 xcell.style.setattr(field,

/usr/lib/python3/dist-packages/pandas/io/excel.py in _convert_to_style(cls, style_dict)
583 .setattr('border_style', nv))
584 else:
--> 585 xls_style.getattribute(key).setattr(nk, nv)
586
587 return xls_style

/usr/local/lib/python3.4/dist-packages/openpyxl/styles/hashable.py in setattr(self, _args, *_kwargs)
51 getattr(self, name) is not None
52 and not isinstance(getattr(self, name), Descriptor)):
---> 53 raise TypeError('cannot set %s attribute' % name)
54 return object.setattr(self, _args, *_kwargs)
55

TypeError: cannot set bold attribute

It appears to me that there seems to be sufficient differences in openpyxl between Python2 and Python3. Something the pandas devs might want to be aware of.

@jreback
Copy link
Contributor

jreback commented Dec 12, 2014

@upthewazoo you have a fairly old version of pandas w.r.t. these types of features. They have been updated in 0.14.* and 0.15.*

cc @neirbowj

This looks like a newwer version of openpyxl (which breaks the API yet again?)

@jreback jreback added IO Excel read_excel, to_excel Compat pandas objects compatability with Numpy or Python functions labels Dec 12, 2014
@neirbowj
Copy link
Contributor

openpyxl 2.1.1 through 2.1.4 have all been released since I last checked for breakage. I'll take a look but the first step to resolve this issue is either to use an expected-compatible pair of versions; upgrade pandas or downgrade openpyxl.

@neirbowj
Copy link
Contributor

There are a few changes to the styles module from 2.1.0 to 2.1.1, but nothing that I would expect to cause pandas breakage. styles is untouched in 2.1.2 and 2.1.3. 2.1.4 is mentioned in the docs but hasn't actually been tagged in bitbucket nor published to pypi.

@upthewazoo
Copy link
Author

Thank you for doing some digging and getting back to me.

The instructions on the pandas website for installation (Linux: Debian/Ubuntu) was used sudo apt-get install python3-pandas so the Ubuntu distribution's repositories in 14.04.1 are still with 0.13.x branch of pandas. The repositories took care of dependencies but did not install openpyxl and would you know, the python2 version of openpxyl (python-openpyxl) is in the repositories but the python3 version (python3-openpyxl) is not in the repositories so I gave up on the Ubuntu 14.04.1 repositories and fell back to using pip (pip3) to install which got me the latest version of openpyxl causing my issue. Installing python3-pandas by hand with all required and optional dependencies is going to take quite a while so I'm going to look at using pip3 to fix the openpyxl package installation.

Note that the pandas documentation still mentions "openpyxl version 1.6.1 or higher" for the 13.1 branch installation requirements for openpyxl (the page named install.html).

But the 14.1 version of the install.html page puts an upper bounds cap on the requirement "openpyxl version 1.6.1 or higher, but lower than 2.0.0".

It would help us repository installers who have to install openpyxl by hand with pip to have an upper version cap added to the pandas website 13.1 documentation (install.html) so we can feed pip the parameter to install a version of openpyxl less than or equal to the upper bound for 13.1 branch's ability to support the API of openpyxl.

I can manually do this right now for my situation, I may play with the 14.1 upper bounds limit to see if it still works in 13.1 branch of pandas.


$ sudo pip3 uninstall openpyxl
$ sudo pip3 install "openpyxl>=1.6.1,<2.0.0"
Downloading/unpacking openpyxl>=1.6.1,<2.0.0
Downloading openpyxl-1.8.6-py2.py3-none-any.whl (190kB): 190kB downloaded
Installing collected packages: openpyxl
Successfully installed openpyxl
Cleaning up...

$ipython3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
Type "copyright", "credits" or "license" for more information.

IPython 1.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: import openpyxl

In [2]: openpyxl.__version__
Out[2]: '1.8.6'


Rerun my ipython3 notebook

--->diff_output[(diff_output.has_change == 'Y')].to_excel('my-diff.xlsx')

Executes without returning an error inside the ipython3 notebook!
Output excel file looks good!

Cheers!

@neirbowj
Copy link
Contributor

For reference, pandas has failed informatively on openpyxl>=2.0.0 (#7169) since 0.14.0, failed gracefully (#7169) since 0.14.1, learned the new openpyxl.styles API (#7177) and handled backwards-incompatible changes to openpyxl.styles.numbers.NumberFormat as of 2.1.0 (#8342) in 0.15.0.

@upthewazoo
Copy link
Author

Good to know, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions IO Excel read_excel, to_excel
Projects
None yet
Development

No branches or pull requests

3 participants