How to print all the chart properties in Python-pptx - python-3.x

I have a existing chart in pptx file, i just want to update its values on daily basis, these charts are already plotted from excel and paste into pptx, i just want to modify the charts ,
but i don't know how many properties are there in charts like series, category and title properties.
i am able to print the chart series and category data, but when i modify these with chart.replace_data(chart_data)
i am getting error like below
return self._chart_part.related_parts[xlsx_part_rId]
KeyError: 'rId4'
I ma using below code to print the values ,
for series in chart.plots:
for i in series.categories:
cat_list.append(i)
for series in chart.series:
for value in series.values:
ser_list.append(value)
the graph looks like below
How can i modify such graphs with updated values which i will be passing from excel to pptx.

I suspect the problem is rooted in the "pasting from Excel" step.
One thing you can try is to right-click on the chart and choose "Edit values in Excel". If that doesn't work for any reason, that would explain the error you're seeing. If it does work, try saving and run the python-pptx code against the saved version.
An alternative, perhaps more reliable fix would be to recreate the chart natively in PowerPoint, starting from adding a new column-chart and adjusting it until you get what you see here. Then you should be able to update it daily with pretty much the code you mentioned (although your ser_list variable will accumulate values for all series in the chart if there is more than one).
To understand why this could be happening, it's important to observe that a chart does not have to have an Excel worksheet "behind" it. It can, and perhaps does in this pasting case, simply have fixed literal values that are embedded in the chart XML itself. The up-side of this is the pasting operation doesn't need to create a whole Excel spreadsheet, which might be tricky in a cut-and-paste context. The down-side is the values can't be conveniently changed using the standard methods.
I haven't tried working with one of those lately so I don't know if editing and saving the data adds a new Excel object created from the embedded values or not, but this might explain the behavior you're seeing.

Related

Excel Line Chart with separate data points for a single line

I have a weird problem that google's not being too kind on results (mostly basic things on how to create a line chart, or combination charts, neither of which I'm after).
I'm trying to automate a report we do each month, I've managed pretty well so far by having a linked excel chart and objects, I have a single sheet in excel I paste my formatted data (set format is consistent each month) then excel picks out the data, creates the charts and when opening powerpoint it reads it all in from there.
Great! Until I was given a new piece of information, there's monthly data and a yearly tally in my data, monthly data is shown with a particular "weighting" applied, year with a different one and it's this that's caused a problem.
The package that's exporting the data can't show these figures contiguously, so we have an export with all the monthly weights, then with annual weights.
So say originally we were looking at C83 to O83 for our data for this year, we're now looking at C83 to N83 and O1364.
I've tried to look at how I would add this to the chart, which currently has the reference:
='Monthly Data to Update'!$C$83:$O$83
But doing anything + or & doesn't seem to work.
So, I decided to just pull all my data in to contiguous set to reference, (so ='Monthly Data to Update'!C83 one cell, ='Monthly Data to Update'!C84 next cell and so on).
This caused a problem for future months that're empty, having 0's in them, so I updated my code to check:
=IF('Monthly Data to Update'!C83=0,NA(),'Monthly Data to Update'!C83)
And this works, my table below my chart is blank for the empty months, my chart doesn't try and plot these empty months. Hurrah!
Now, the wrinkle. The Yearly nets aren't charted but are displayed in the table (I'm not up on Excel charts so how this was originally setup, I just copied the charts from Powerpoint in to Excel and reset the data to where it needed to be before copying it back to Powerpoint as a data linked chart).
First chart I did, works fine, second chart I did it's decided to start plotting the annual net for two of the three lines = \
So, this is how my chart looks like, running off the contiguous (but wrong) data:
And this is how it looks running off the fetched contiguous (correct) data:
It's really baffling, Exceeded and Met now plot the Year to Date figure, whereas Not Met behaves as it should, and they all have the same data layout (on the right) using the same formulas (just pointing at different cells).
I am confused..
I'm hoping to keep this light and simple so have avoided using any macro's to date, so that down the line when I hand this project over to someone any maintenance should be easy to do (say add a chart) without them asking me and me then having to remember what the hell I did a few months ago.. - so if this is doable without going down that line (which, seems like it should be, given my first chart the two lines there worked fine, and one of the three in this is behaving itself).
Apologies for the long winded description, I've given full history so if there's a step further back I could've done better, then happy to fix it up there rather than where I got to.
Ah, so apparently I could've had non-contiguous data with a comma, not a plus or ampersand, so the chart data would be:
=('Monthly Data to Update'!$C$83:$N$83,'Monthly Data to Update'!$O$1404)
So I don't need to rebuild the data in to a contiguous set, and my charts don't cock up when selecting the data this way, win's all around.
Thank you for your time, hopefully this helps someone in the future. (as a saving grace for my lost morning)

Programmatically add rows to an Excel data model via C#

We're looking at allowing our customers to download an Excel file from our web application which contains a raw export of their data along with some basic charts and pivot tables based on that data.
The basic way, we want to make this work is that we have a fixed Excel file which contains all the reporting elements in one worksheet and have room for the underlying data in another worksheet. When the user requests their Excel report, we programmatically fill out the data worksheet with their results and send them the final Excel file.
Everything seemed a bit to easy when doing the prototyping with a fixed set of data. The dataset we worked with was added to the Excel Data Model and we then set up the charts and other reporting elements. However, when using that file as the template for the generated Excel file in our application we are finding that the definition of the data model still remains - meaning, that we built the "protype" with a table definition of $A$1:$T$5879
but when generating the report, that definition isn't changed to contain whatever size the added dataset might have.
We're using EPPlus to work with the generation of our Excel sheets and have so far been unable to find any sort of solution to this kind of problem. This might very much be due to us being quite Excel novices. The goal is to have the user experience, that the charts and pivot tables contained in the Excel sheet reflects the total dataset contained in the Excel file without them having to do anything.
Ok, I've actually found a solution for it.
The solution was right infront of us.
We define the dataset as a named set - this is done under under the "Formulas" and inside the "Name Manager". We have a range which defines our dataset - the "Refers To" field when defining a range can take a formula. So intead of giving it a fixed size, we use this: =OFFSET(Data!$A$1;0;0;COUNTA(Data!$A:$A);COUNTA(Data!$1:$1))
This counts the amount of rows and columns, with reference to A1 in our Data worksheet. All our pivots are set to reload on startup and that seems to work.

Unlink chart from excel and keep data editable?

for the longest of times I've been trying to achieve this to no avail. It seems like it should be a common problem but still I haven't been able find an answer. What I want to do is:
Have a chart in Excel and/or Powerpoint.
Copy this chart as a chart object into PPT.
Keep all characteristics of the original chart but maintain it editable (i.e. - add/remove axes, data labels etc)
Break the link to the original chart/workbook but keep the data editable. I.e., I want a new excel sheet opened when I want to edit the data, just like it works if I create a new chart object.
Preferably, I would like to convert the data of the original to this new chart sheet but this is not of really high importance.
Basically, I want to be able to use graphs as templates for a new graph without editing the original data. Please help!
PS. I didn't include any code as most of the code I've tried has tried to achieve something else. I mostly want to know if this is even possible to do (it should be!)?

See the code of a graph that is already created

I have a graph in Excel and want to use the exact format, size, axes, title etc. I know that I can just record a macro of me recreating a similar graph, but is there a way to step into the code of the one already created?
You can run the existing code. You'll have to adjust some of the hard-coded parts of the code, since the data range and the chart object name of the new chart will be different.
If you post the recorded code, someone might be able to help with this.

load VBA data to line chart

I need to feed data into a line chart with VBA. I need to give a basic unidimensional range when I do it with Excel in a raw test. The thing is that now I need to hide some things in VBA code, so that the user just sees a curve resulting of some basic input at the click of a button.
I think there are at least two options for me :
either have the chart object placed in the Excel before, and having it point to some VBA snippet. In this case, should I somehow point to VBA from Excel ? Or would VBA access the chart area by some ID ?
create everything dynamically from VBA, the chart window, the chart, and the series input. This might be trickier to figure out maybe as I have frozen panes, so there would be the question of which pane is active (I'd like to avoid to have to do Selects to select the right one)
What would you recommend ? And in either case, how to actually do it ? What type of data to put in ?
Thank you so much !
I was involved in a project a couple of years ago that might be relevant.
A report was created monthly for the organisation's management board which contained dozens of charts. It was a near full time job for a statistician to collate and process data from across the organisation that provided the source data for these charts. I was asked to automate what could be automated.
It became clear that the appearance of the chart could not be automated. The statistician and the management board's staff held regular discussions and often agreed minor changes which they thought would better present the organisation's performance.
I could automate the collection of the data, its processing and placing of the results in the ranges from which the charts got their data. The most I ever did with a chart was change the size of a source range.
What would be the parameters that would control your macro's changes to a chart? How would your macro get the determine: type of chart, nature of the axes, text, etc?
Hope these thoughts help. I could look out my notes if my experience seems particularly relevant.

Resources