xlsxwriter.Workbook AttributeError: 'module' object has no attribute 'Workbook'
Asked Answered
R

4

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.

Rust answered 20/10, 2016 at 10:5 Comment(2)
xlsxwriter version?Tati
xlsxwriter version is 0.9.3Rust
O
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!

Ossy answered 26/3, 2017 at 19:3 Comment(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 fileSubmarginal
C
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.

Chickadee answered 15/3, 2021 at 11:49 Comment(1)
copy.py is a Python standard library so it is probably best to avoid that as a user defined module name.Voracity
W
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.

Wrought answered 19/7, 2019 at 20:52 Comment(0)
M
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()
Meperidine answered 5/1, 2023 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.