简体   繁体   English

Python使用XlsxWriter将多个矩阵写入excel

[英]Python write multiple matrix into excel using XlsxWriter

I need to write multiple matrices in Excel using XlsxWriter. 我需要使用XlsxWriter在Excel中编写多个矩阵。

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? 如何指定可以插入对应的daraframe的单元格的位置?

To shift the output of the DataFrame within the worksheet, use the named arguments startrow and startcol in your call to to_excel() . 要在工作表中移动DataFrame的输出,请在对to_excel()调用中使用命名参数startrowstartcol In the following example, the output is placed with the upper left cell in E3. 在以下示例中,将输出放置在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()

I would like to share my code that is able to write a matrix (list of lists) into an Excel file. 我想分享我的代码,该代码能够将矩阵(列表列表)写入Excel文件。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM