i want to plot data, that i gathered in Python, in Excel.
And if the file already exists, it should just add another Sheet.
But when i try to add a file it gives me the Error "workbook" object has no attribute "add_chart"
Here is the code that i got:
import pandas as pd
import openpyxl
import xlsxwriter
from pathlib import Path
def Convert(self):
path_to_file = '{0}.xlsx'.format(self.Entry_ExcelName.get())
path = Path(path_to_file)
excel_file = '{0}.xlsx'.format(self.Entry_ExcelName.get())
if path.is_file():
writer = pd.ExcelWriter(excel_file, engine='openpyxl', mode='a')
else:
writer = pd.ExcelWriter(excel_file, engine='xlsxwriter')
list_data_conv = self.list_data
df=pd.DataFrame(list_data_conv)
sheet_name = '{0}'.format(self.Entry_SheetName.get())
df.to_excel(writer, sheet_name=sheet_name)
workbook=writer.book
worksheet=writer.sheets[sheet_name]
chart=workbook.add_chart({'type': 'line'})
i=len(list_data_conv)
chart.add_series({
'categories': ['{}'.format(sheet_name) ,1,1,i,1],
'values': ['{}'.format(sheet_name) ,1,2,i,2]
})
chart.set_x_axis({'name': 'Index', 'position_axis': 'on_tick'})
chart.set_y_axis({'name': 'Value', 'major_gridlines': {'visible': False}})
chart.set_legend({'position': 'none'})
worksheet.insert_chart('D2', chart)
writer.save()
I know there is already a similiar question like mine, however that has not fixed my problem.
I feel like i can't see the forest for the trees.