1

For example, I have the following code where I am trying to write array a to excel file

a = array([1, 2, 3])
write_excel(a)

The function that I wrote is as follow:

def write_excel(numpy_array):
    workbook = xlsxwriter.Workbook('C:\\Users\\GTS\\Desktop\\Network \\rel.xlsx')
    worksheet = workbook.add_worksheet()

    for row, data in enumerate(numpy_array):
        worksheet.write_row(row, data)

    workbook.close()

I received the following error:

Traceback (most recent call last):
  File "C:/Users/GTS/PycharmProjects/Rel1/Rel1.py", line 18, in <module>
    write_excel(a)
  File "C:\Users\GTS\PycharmProjects\Rel1", line 346, in write_excel
    worksheet.write_row(row, data)
  File "C:\Users\GTS\PycharmProjects\Rel1\venv\lib\site-packages\xlsxwriter\worksheet.py", line 69, in cell_wrapper
    return method(self, *args, **kwargs)
TypeError: write_row() missing 1 required positional argument: 'data'
1

3 Answers 3

0

You can call write_row() directly with a numpy array. Like this:

def write_excel(numpy_array):
    workbook = xlsxwriter.Workbook('test.xlsx')
    worksheet = workbook.add_worksheet()

    worksheet.write_row(0, 0, numpy_array)

    workbook.close()
Sign up to request clarification or add additional context in comments.

Comments

0

write_row function takes arguments in the form of (row, col, data). So use worksheet.write_row(row, 0, data) instead.

1 Comment

thank you, but I still received the following error: line 69, in cell_wrapper return method(self, *args, **kwargs) line 1132, in write_row for token in data: TypeError: 'numpy.int32' object is not iterable
0

I wrote this function instead it works very well

def write_excel_one(numpy_array):
    workbook = xlsxwriter.Workbook('C:\\Users\\GTS\\Desktop\\Network Interdiction Problem\\Manuscript\\rel.xlsx')
    worksheet = workbook.add_worksheet()

    for row, data in enumerate(numpy_array):
        # worksheet.write_row(row, 0, data)
        worksheet.write_number(0, row, data)
    workbook.close()

Comments

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.