I am using Openpyxl to write several tabs/sheets to an excel file. Why I chose Openpyxl over xlswriter is because I would like to load an existing workbook and create a bar chart using specific columns and rows.
The problem is the bar chart is positioned by default over the data and I would like to position it either below the data or to the right side of data.
Are there parameters that we can use to move the position of the container which holds the bar chart.
We can do this in xlswriter using x_offset and y_offset parameters while inserting the chart. Is there similar functionality with openpyxl.
Any help would be appreciated.
You can position the chart in a cell. For example, the following chart would be inserted in cell B5.
ws.add_chart(chart, "B5")
The top left of the chart is now anchored to B5. You can change the anchor parameter to another cell reference.
Related
I am drawing an excel scatter chart with python and xlsx writer and use custom data labels for one series. But I can not set the position of the custom labels like I can with the default labels. If I set the position of the custom labels they dont change in the chart. In the debugger or if I check their label options in excel itself I can see the changed position values, but they are not applied in the drawing.
At default the custom labels seem to bet set at right. I want them on top but I cant get this. The code is like that:
chart.add_series( .., 'data_labels': {'custom': my_custom_labels, 'position': 'above'})
But the changes wont appy to the chart.
I also found i can set the default label position (label_position_default) in the chart object (which is right on default) but it wont change the custom label position either.
Anyone has an idea how to get a custom data label on top position?
I'd like to delete plot that is in the chart space. Does any one know how to do that with pptx python? Since there's option to delete plots in powerpoint GUI. Is there any way I can do that with python pptx?
I have four charts in a slide and I want to first store the shapes of plots and then delete the plots inside of the charts.
Reading the docs I found this paragraph that shows in more detail how plots work:
A plot is like a sub-chart, containing one or more series and drawn as
a particular chart type, like column or line. This distinction is
needed for charts that combine more than one type, like a line chart
appearing on top of a column chart. A chart like this would have two
plot objects, one for the series appearing as columns and the other
for the lines. Most charts only have a single plot and python-pptx
doesn’t yet support creating multi-plot charts, but you can access
multiple plots on a chart that already has them.
From your question I assume that you want to delete the plot to replace it with a new one. I suggest that you delete the series inside the existing plot and add to it the new series you need.
I am trying to create histograms in multiple sheets of an excel workbook. I can very easily crate chart using pandas or matplotlib libraries and show it in python output window. However I would like to plot the histogram chart in excel. I am using office 360. Is there a way to do so?
You can create a histogram chart with XlsxWriter using an Excel "Column" chart type: Column chart example
See also the Working with Charts section of the XlsxWriter docs.
I have an Excel file with multiple sheets. With the data I retrieve from another Excel file, I want to add matplotlib charts to each sheet. However, although the plots are generated and shown in the console while the loop runs, once the loop is finished, all the sheets have the last chart added; the one that's supposed to be added to the last sheet.
I tried breaking down the loop and going manually, e.g., plot scheme for sheet[0], for sheet[1], etc. This gave the same result. I don't know if I'm doing something wrong or this is some sort of bug in openpyxl.
sheets = wb.sheetnames
for i in range(0, len(sheets)):
#Iterate over sheets
ws = wb[sheets[i]]
#The chart code
. . .
#I save the current plot to be added to the Excel sheet using openpyxl
fig1.savefig("myplot.png", dpi = 72)
#Add the saved chart to the active excel sheet, which is the current iteration in the loop
img = openpyxl.drawing.image.Image('myplot.png')
ws.add_image(img)
wb.save('Excel_file.xlsx')
At each iteration a new chart is supposed to be added to the active sheet. But I get the last chart in all of them at the end.
The Excel file with the charts added is supposed to be the final form of a fully automated report. I am not using openpyxl's charts instead of matplotlib because I need the flexibility and customizability of the latter.
Any help is appreciated. Many thanks.
I was successfully able to generate Stacked Column charts in the newly created Excel sheet using pandas dataframe with xlsxwriter of Python Pandas. But, I can't figure out how to assign color yet.
Here is the picture.
This is from Pandas xlsxwriter documentation. In the given picture, each "Metric" has different colors. For Example, Metric 8 is pink and Metric 1 is blue. I want to assign specific colors to each metric in this example. Obviously, each metric belongs to each row of data in question.
I understand I can do this in excel individually. But, I am writing Python code to generate several dozens of stacked column charts and put it in excel using xlsxwriter. So, it is not practical to do this by hand.
Any help is appreciated !
You need to set the fill color for the series.
See the following Pandas-XlsxWriter stacked charts with colors example. The example uses brew colors but you can replace those with any Html like RGB color.
See also the XlsxWriter Chart documentation and Working with Charts which explain the API and give examples of setting the colors of different properties.