4

Getting error "AttributeError: 'int' object has no attribute '_get_xf_index'" while writing from array

Tried a lot of recommendation from Google, but nothing seems to work for me.

import xlsxwriter

# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Report.xlsx')
worksheet = workbook.add_worksheet()

# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': 1})

# Adjust the column width.
worksheet.set_column(15, 15, 15, 15)

# Write some data headers.
worksheet.write('A1', 'Name', bold)
worksheet.write('B1', 'Description', bold)
worksheet.write('C1', 'Products', bold)
worksheet.write('D1', 'Author', bold)

# Some data we want to write to the worksheet.
data = (
['Test-1', 'Confirms that API can be called with valid parameters.',    'x, y, x', 'Sam'],
['Test-2', 'Confirms that API can be called with valid parameters.', 'x, y, x', 'Sam'],
['Test-3', 'Confirms that API can be called with valid parameters.', 'x, y, x', 'Sam'],
['Test-4', 'Confirms that API can be called with valid parameters.', 'x, y, x', 'Sam'],
)

# Start from the first cell below the headers.
row = 1
col = 0

for name, description, products, author in data:
    worksheet.write_string  (row, col, name)
    worksheet.write_string  (row, col + 1, description)
    worksheet.write_string  (row, col + 2, products)
    worksheet.write_string  (row, col + 3, author)
    row += 1

workbook.close()

Error Messages :-

Traceback (most recent call last):
  File "D:\VCI_TESTSUITES\rt-testsuite-working\rt-testsuite-tests\Data\Tests\ProcessingPrograms\XMLReportGenerator - Copy.py", line 38, in <module>
    workbook.close()
  File "C:\Users\iqs1sba\AppData\Local\Programs\Python\Python37\lib\site-packages\xlsxwriter-1.2.2-py3.7.egg\xlsxwriter\workbook.py", line 311, in close
  File "C:\Users\iqs1sba\AppData\Local\Programs\Python\Python37\lib\site-packages\xlsxwriter-1.2.2-py3.7.egg\xlsxwriter\workbook.py", line 674, in _store_workbook
  File "C:\Users\iqs1sba\AppData\Local\Programs\Python\Python37\lib\site-packages\xlsxwriter-1.2.2-py3.7.egg\xlsxwriter\packager.py", line 135, in _create_package
  File "C:\Users\iqs1sba\AppData\Local\Programs\Python\Python37\lib\site-packages\xlsxwriter-1.2.2-py3.7.egg\xlsxwriter\packager.py", line 190, in _write_worksheet_files
  File "C:\Users\iqs1sba\AppData\Local\Programs\Python\Python37\lib\site-packages\xlsxwriter-1.2.2-py3.7.egg\xlsxwriter\worksheet.py", line 3772, in _assemble_xml_file
  File "C:\Users\iqs1sba\AppData\Local\Programs\Python\Python37\lib\site-packages\xlsxwriter-1.2.2-py3.7.egg\xlsxwriter\worksheet.py", line 5209, in _write_cols
  File "C:\Users\iqs1sba\AppData\Local\Programs\Python\Python37\lib\site-packages\xlsxwriter-1.2.2-py3.7.egg\xlsxwriter\worksheet.py", line 5224, in _write_col_info
AttributeError: 'int' object has no attribute '_get_xf_index'
1
  • Please help me resolve this error. I am new to Python & xlsxwriter Commented Nov 5, 2019 at 21:21

1 Answer 1

7

The problem is in the set_column method. You're calling it with the wrong syntax. It's set_column(self, first_col, last_col, width=None, cell_format=None, options=None)

In your case, that would be

worksheet.set_column(0, 3, 15)
Sign up to request clarification or add additional context in comments.

4 Comments

Wow, I just tried that and it worked like magic. Thank you so much for helping me out.
I am trying to understand the numbers. self = 0, first_col = 3, last_col = 15. 0 I understood, but 3 & 15. Please give me a little for information.
self is not an actual parameter that you need to pass, it's just a keyword to reference class instance. So, it's first_col = 0, last_col = 3, width = 15
I see. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.