从数组写入时出现错误“AttributeError:'int'对象没有属性'_get_xf_index'”
尝试了谷歌的很多推荐,但似乎没有什么对我有用。
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()
Run Code Online (Sandbox Code Playgroud)
错误消息:-
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()
Run Code Online (Sandbox Code Playgroud)
小智 7
问题出在set_column方法上。您使用错误的语法调用它。它是set_column(self, first_col, last_col, width=None, cell_format=None, options=None)
在你的情况下,那就是
worksheet.set_column(0, 3, 15)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5573 次 |
| 最近记录: |