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

标签 python excel xlsxwriter

我需要使用 XlsxWriter 在 Excel 中写入多个矩阵。

但是我想提前指定矩阵的位置

这是我的代码

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

如何指定可以插入相应 daraframe 的单元格位置?

最佳答案

要在工作表中移动 DataFrame 的输出,请在调用 to_excel() 时使用命名参数 startrowstartcol。在以下示例中,输出放置在 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()

关于Python使用XlsxWriter将多个矩阵写入excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34417106/

相关文章:

python - 以适用于str和unicode的方式使用translate的python方法是什么?

excel - 如何将单个单元格拆分为多行并添加另一行

python - 每隔一行遮荫

python - df.to_csv 适用于 S3 存储桶,但 df.to_excel 不适用

python - 在 python 中使用 numpy 对 100x100 数组进行排序

Python logger.debug 将参数转换为字符串而不记录日志

vba - 更改调用单元内部颜色

excel - 使用 Talend 将文本文件转换为 Excel

python - 如何使用 xlsxwriter 设置 x 轴数据绘制 pandas 数据框

python "If max number of steps is exceeded, break the loop"