Forum Archive

Openpyxl problem

ihf

I am able to import openpyxl but when I try to run a sample script I get an error which I could use some help understanding.

The script:

from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['A1'] = 42
ws.append([1, 2, 3])
import datetime
ws['A2'] = datetime.datetime.now()
wb.save("sample.xlsx")

at the last line wb.save, I get an error in openpyxl/writer/worksheet.py, line 192, in write_worksheet to the effect
with xmlfile(out) as xf:
TypeError: 'module' object is not callable.

Any help is much appreciated.

ccc

Line 192 of https://bitbucket.org/openpyxl/openpyxl/src/59d472f008dbbe2f99c487879709204115090e93/openpyxl/writer/worksheet.py is the issue. It thinks that xmlfile is a function when it is a module.

Line 20 of worksheet.py imports xmlfile from https://bitbucket.org/openpyxl/openpyxl/src/59d472f008dbbe2f99c487879709204115090e93/openpyxl/xml/functions.py

Line 16 of functions.py is looking to see if the lxml module is present. Lxml is NOT present in Pythonista so line 51 imports xmlfile from et_xmlfile.

At this point I could not find et_xmlfile. ET phone home.

ihf

Thank you. I found et_xmlfile, and added it to the library and now this simple script works.

cronywalls

This error statement TypeError: 'module' object is not callable is raised as you are being confused about the Class name and Module name. The problem is in the import line . You are importing a module, not a class. This happend because the module name and class name have the same name .

If you have a class "MyClass" in a file called "MyClass.py" , then you should import :

from MyClass import MyClass

In Python , a script is a module, whose name is determined by the filename . So when you start out your file MyClass.py with import MyClass you are creating a loop in the module structure.