[英]Add a new sheet to existing excel file and graph the data
I am looking to add a new sheet to an excel file by exporting a pandas dataframe and graph the data using openpyxl. 我想通过导出熊猫数据框并使用openpyxl绘制数据图形,将新工作表添加到excel文件中。 Currently I can add a new sheet and insert the pandas dataframe but not the graph of the data. 目前,我可以添加一个新工作表并插入pandas数据框,但不能插入数据图。
Currently my code looks like this. 目前,我的代码如下所示。
import pandas
import openpyxl
from openpyxl.chart import LineChart, Reference
fileSavePath = 'C:/Users/PATH'
filename = "Existing Excel File"
lst1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
lst2 = [0, 5, 2, 8, 15, 7, 10, 4, 0]
df = pandas.DataFrame({'First Column': lst1, 'Second Column': lst2})
book = openpyxl.load_workbook(fileSavePath + filename + '.xlsx')
with pandas.ExcelWriter(fileSavePath + filename + '.xlsx', engine='openpyxl') as writer:
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, 'new_sheet', index=False)
wb = openpyxl.Workbook()
sheet = wb.active
values = Reference(sheet, min_col=1, min_row=1,
max_col=1, max_row=len(lst1))
chart = LineChart()
chart.add_data(values)
chart.title = " LINE-CHART "
chart.x_axis.title = " X-AXIS "
chart.y_axis.title = " Y-AXIS "
sheet.add_chart(chart, "E2")
writer.save()
writer.close()
Currently there is no error and the dataframe writes in but the graph does not appear in the newly added sheet. 当前没有错误,并且数据帧已写入,但图形未出现在新添加的工作表中。
This code will successfully add a new sheet and graph the data. 此代码将成功添加新的工作表并绘制数据图形。
import pandas
from openpyxl.chart import LineChart, Reference
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl import load_workbook
lst1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
lst2 = [0, 5, 2, 8, 15, 7, 10, 4, 0]
df = pandas.DataFrame({'First Column': lst1, 'Second Column': lst2})
fileSavePath = 'C:/Users/PathExample'
filename = "filename"
newSheetName = 'newSheet'
sheet = newSheetName
# load in existing workbook
wb = load_workbook(fileSavePath + filename + '.xlsx')
# create new sheet with name assigned by title
wb.create_sheet(newSheetName)
# find which sheet is the new sheet
newSheetIndex = len(wb.sheetnames) - 1
# set the newest sheet as the active sheet
wb.active = newSheetIndex
worksheet = wb.active
# Insert Dataframe
for r in dataframe_to_rows(df, index=False, header=True):
worksheet.append(r)
#Graph data
chart = LineChart()
data = Reference(worksheet, range_string=f'{sheet}!B2:B10')
category = Reference(worksheet, range_string=f'{sheet}!A2:A10')
chart.add_data(data, titles_from_data=True)
chart.set_categories(category)
worksheet.add_chart(chart, 'D3')
wb.save(fileSavePath + filename + '.xlsx')
The issue appears to be in setting the active sheet. 问题似乎出在设置活动工作表上。 The previous code added the sheets but kept graphing the "active sheet" which is why the graph did not appear on the other sheets. 先前的代码添加了工作表,但是继续绘制“活动工作表”的图形,这就是为什么该图未出现在其他工作表上的原因。 Openpyxl uses a list to keep track of the excel sheets so to manipulate a desired sheet you need to set that index number as the active sheet (wb.active). Openpyxl使用一个列表来跟踪excel工作表,因此要操作所需的工作表,您需要将该索引号设置为活动工作表(wb.active)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.