Python Forum
Openpyxl overwrite Linechart
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Openpyxl overwrite Linechart
#1
Hi All,
Is there any way to overwrite linechart in excel?It get more chart in excel during running *.Py code. Please refer to attachment file.
If have a way to delete exist chart and then run code once get a new chart, thanks in advance!



import openpyxl
from openpyxl.chart import Reference, LineChart, Series

wb = openpyxl.load_workbook('wb2.xlsx')
sheet = wb.active

# Data for plotting
# Choose all the data from Column 2 to 4
values = Reference(sheet,
                   min_col=2,
                   max_col=10,
                   min_row=1,
                   max_row=38)

# Create object of LineChart class
chart = LineChart()
chart.add_data(values, titles_from_data=True)
# set the title of the chart
chart.title = "Analysis Stock prices"
# set the title of the x-axis
chart.x_axis.title = "Date"

# set the title of the y-axis
chart.y_axis.title = "Stock Value"

# the top-left corner of the chart
# is anchored to cell F2 .
sheet.add_chart(chart,"F2")

# save the file
wb.save("wb2.xlsx")

Attached Files

Thumbnail(s)
   

.xlsx   wb2.xlsx (Size: 11.59 KB / Downloads: 17)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Openpyxl make LineChart SamLiu 0 303 Nov-22-2022, 06:54 AM
Last Post: SamLiu
  Overwrite previous live data. Makada 2 1,660 Nov-07-2020, 07:40 PM
Last Post: Makada
  How to overwrite file SriRajesh 3 5,251 Apr-14-2020, 12:57 PM
Last Post: steve_shambles
  Dict overwrite previous list vincentspm 1 1,722 Mar-20-2019, 12:31 PM
Last Post: scidam
  overwrite data using openpyxl BNB 0 9,741 Jun-21-2017, 11:31 AM
Last Post: BNB

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020