0

I am using Openpyxl package to append rows(numeric data) in an excel sheet through python script. The excel document consists of 3 sheets with graphs and charts in other sheets. So, whenever the python script is being run and I open the excel to see the changes, the charts and graphs in other sheets are deleted and shows error. Please find the screenshot attached.

Error

Can anyone please let me know how I can avoid this and is there any feature in openpyxl which will not overwrite existing chart data in excel sheet?

Sourabh
  • 23
  • 7

1 Answers1

0

When you use openpyxl to append rows, if the graph or charts is on the row that will be append by your code. You need to reassign a new cell for them first. And for openpyxl, for example you have a graph from 'A1' to 'C5', the 'A1' will be the cell to contain the graph, other cells like 'A2','B3' will be none. I think that might be the cause of the deletion of your graphs. And you need to close the excel file when the script is running, only open it after the script is done.

And use the exact sheet you want to make change on:

    wb = openpyxl.load_workbook('filename.xlsx')
    wb1 = wb['sheetname']

I will use statement like the one below to move the location of an existing element.

sheetname['A1'].value = sheetname['B1'].value

And here is the link to a similar question.

Zichzheng
  • 1,090
  • 7
  • 25
  • Actually, the charts and graphs are in different sheets. The excel document consists of 3 sheets. Openpyxl is appending rows in 1st sheet but graphs and charts data in other sheets are modified. I have edited my original question. Thank you for the quick response – Sourabh Jun 17 '21 at 17:20
  • Aha, that's weird. Did you modify the data with the sheet object. I updated the answer. – Zichzheng Jun 17 '21 at 17:32
  • Initially I am checking if some columns are present in excel and if they are available then new data is appended in that sheet. I am not modifying any data in the sheet which contains graphs and charts... – Sourabh Jun 17 '21 at 18:17
  • Can you post your code in the question section? It's very unusual for openpyxl to make change on a sheet that you didnot not call in the code. You can also try to save the new result to a new xlsx to see what will you get via `openpyxl.Workbook().copy_worksheet()` – Zichzheng Jun 17 '21 at 18:35
  • I have attached screenshot of error message from excel for your reference.. – Sourabh Jun 18 '21 at 07:30