将pandas数据框写入Excel命名范围

时间:2019-12-20 14:45:57

标签: python pandas dataframe openpyxl

我需要加载现有的Excel工作表,将pandas数据框写入指定的范围,然后将工作表保存为新名称。我应该怎么做?

import openpyxl
import pandas.util.testing as tm
from openpyxl import Workbook, load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows

# Load Excel sheet, which contains the named range ('TestRange') in range A1:C2
wb = load_workbook('template.xlsx')
ws = wb['Tab1']

# Create dummy 2x3 dataframe
tm.N, tm.K = 2, 3
df = tm.makeDataFrame()

# Write dateframe to 'TestRange'
# That's the part in question!

# Save sheet under new name
wb.save('df1.xlsx')

0 个答案:

没有答案