6

Saw this article Detail AttributeError: 'module' object has no attribute 'workbook' for the same and the error there was a typo."W" is uppercase for Workbook. Also the package used is xlwt.

I use Python 2.7 in unix. Installed XlsxWriter and used as below(Same example as given in the official page http://xlsxwriter.readthedocs.io/getting_started.html) xlsx version is 0.9.3

import xlsxwriter
workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()

worksheet.write('A1', 'Hello world')

workbook.close()

I use XlsxWriter version 0.9.2.

Please help me figure out what else could be wrong here . Now i am able to get the excel output using xlwt package.

Community
  • 1
  • 1
kten
  • 445
  • 4
  • 13
  • 26

4 Answers4

9

Make sure your file isn't named xlsxwriter.py.

If it is, which was why I got the same error, all import xlsxwriter will do is import the current file, and not the xlsxwriter module installed in your python environment.

Hope this helps!

Alex Zhang
  • 123
  • 1
  • 10
  • 1
    And make sure to clean up *.pyc which will still exist after the module renaming is done. If not, this error occurs, "ImportError: bad magic number in 'xlsxwriter': b'\x03\xf3\r\n'" since the import goes after the*.pyc file – Rich Andrews Apr 06 '19 at 14:43
1

In xlswriter.py we have a module named contenttypes.py. There only they are importing copy.py.

I have a file called copy.py in the same folder. So it was throwing the same error for me. I just changed the file name.

NavaneethaKrishnan
  • 1,213
  • 1
  • 9
  • 22
  • 1
    `copy.py` is a [Python standard library](https://docs.python.org/3/library/copy.html) so it is probably best to avoid that as a user defined module name. – jmcnamara Mar 15 '21 at 13:47
0

I had this problem.

Python is probably not finding the workbook after creating it, or it has changed its name by the time workbook.close() is called. Run the script from the same directory and see if that works out for you.

thlik
  • 401
  • 6
  • 12
0

Use this format: (row, column, content)

import xlsxwriter
workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()

worksheet.write(0, 0, 'Hello world') #(row, column, content) 

workbook.close()
Julia Meshcheryakova
  • 3,162
  • 3
  • 22
  • 42