我需要使用 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()
时使用命名参数 startrow
和 startcol
。在以下示例中,输出放置在 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/