简体   繁体   English

将新工作表添加到现有的excel文件并绘制数据图形

[英]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.

相关问题 将数据添加到现有Excel工作表 - Add data to existing excel sheet 如何使用熊猫将工作表添加到现有的Excel文件中? - How to add sheet to existing excel file with pandas? 如何在Python中将csv文件名作为工作表名称将“ a.csv”文件添加到现有Excel文件“ b.xlsx”作为新工作表? - How to add “a.csv” file to the existing Excel File “b.xlsx” as a new sheet with csv file name as Sheet name in Python? 将新数据添加到现有 excel 文件的更快方法? - a faster way to add new data to an existing excel file? 在不覆盖数据的情况下向现有 excel 文件添加新行(Python) - Add new row to existing excel file without overwriting the data (Python) 添加带有图表的新工作表 - Add new sheet with graph 如何使用 Pandas 在现有 excel 文件中保存新工作表? - How to save a new sheet in an existing excel file, using Pandas? 如何使用 python 在现有 csv 文件中创建新工作表并向其中添加一些数据 - how to create a new sheet in existing csv file using python and add some data into that 如何在 .xlsm 文件中使用 Pandas 将数据框添加到现有 Excel 工作表 - How to add a dataframe to an existing Excel sheet with Pandas, on a .xlsm file 将数据从多个 excel 文件复制到特定工作表上的现有文件 - Copy Data from multiple excel files to existing file on specific sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM