gpt4 book ai didi

python - 使用 xlwings 将数据帧打印到特定的列/行位置,例如 (1,2)

转载 作者:行者123 更新时间:2023-12-01 07:21:59 25 4
gpt4 key购买 nike

尝试找出如何打印到特定的列/行,类似于如何pd.to_excel(startcol = 1, startrow = 1) 有效。我必须在打开的 Excel 工作簿中执行此操作,并找到了 xlwings 库。我目前正在使用 openpyxl,我该如何在 xlwings 中执行此操作?我阅读了打印到特定单元格(如 A1)的文档,但不是通过指定列/行来打印。

#Write to Excel

book = load_workbook('Test.xlsx')
writer = pd.ExcelWriter('Test.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)


def addtoexcel(df, row):
i = 0
df[["val1", "val2"]] = df[["val1", "val2"]].apply(pd.to_numeric)
line = int(df.loc[i][1])
for i in range(1, line+1):
if line ==i:
df = df.T
df.to_excel(writer, "test", index = False, header = False, startcol = line+2, startrow = row)

如何在 xlwings 中通过指定列/行(如 (1,1))进行打印?

最佳答案

您可以使用 xlwings 轻松打印 pandas 数据框以实现卓越。范围对象采用行号和列号作为参数(或仅将单元格引用作为字符串)。考虑以下代码:

import xlwings as xw
import pandas as pd

row = 1
column = 2
path = 'your/path/file.xlsx'

df = pd.DataFrame({'A' : [5,5,5],
'B' : [6,6,6]})

wb = xw.Book(path)
sht = wb.sheets["Sheet1"]

sht.range(row, column).value = df

您还可以添加选项以包含索引/标题:

sht.range(row, column).options(index=False, header=False).value = df

关于python - 使用 xlwings 将数据帧打印到特定的列/行位置,例如 (1,2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57658038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com