2

I need to write multiple matrices in Excel using XlsxWriter.

But I would like to specify the location of matrices in advance

Here is my code

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')

CC1 = Get_Data(test1)    ## get the corresponding matrix
df = DataFrame(CC)     ## put into a dataframe format
df.to_excel(writer, sheet_name="sheet1")    ## write into excel 

CC2 = Get_Data(test2)    ## get the corresponding matrix
df = DataFrame(CC2)     ## put into a dataframe format
df.to_excel(writer, sheet_name="sheet1")    ## write into excel 
writer.save()

How can I specify the location of cell in which I can insert the corresponding daraframe?

2 Answers 2

2

To shift the output of the DataFrame within the worksheet, use the named arguments startrow and startcol in your call to to_excel(). In the following example, the output is placed with the upper left cell in E3.

import numpy as np
import pandas as pd
from xlsxwriter.utility import xl_range

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
workbook = writer.book

df = pd.DataFrame(data=np.random.rand(255))
df.to_excel(
    writer, 'TEST',
    startcol=4,
    startrow=2
)
writer.close()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. It really helps.
2

I would like to share my code that is able to write a matrix (list of lists) into an Excel file.

import xlsxwriter

table = [[a, b], [c, d], [e, f, g]] #table must be your matrix 

workbook = xlsxwriter.Workbook('excelFile.xlsx')
worksheet = workbook.add_worksheet()
col = 0

for row, data in enumerate(table):
    worksheet.write_row(row, col, 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.