AttributeError: 'int' object has no attribute '_get_xf_index'
AttributeError: 'int' object has no attribute '_get_xf_index'
从数组写入时出现错误 "AttributeError: 'int' object has no attribute '_get_xf_index'"
尝试了很多 Google 的推荐,但似乎对我没有用。
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()
错误信息:-
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'
问题出在set_column
方法中。你用错误的语法调用它。这是set_column(self, first_col, last_col, width=None, cell_format=None, options=None)
在你的情况下,那将是
worksheet.set_column(0, 3, 15)
从数组写入时出现错误 "AttributeError: 'int' object has no attribute '_get_xf_index'"
尝试了很多 Google 的推荐,但似乎对我没有用。
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()
错误信息:-
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'
问题出在set_column
方法中。你用错误的语法调用它。这是set_column(self, first_col, last_col, width=None, cell_format=None, options=None)
在你的情况下,那将是
worksheet.set_column(0, 3, 15)