I have an excel file that has a Bar Chart (Chart2) as a sheet named 'Output'. So I want to change the style format of Chart2 by using Openpyxl. However, I tried the simple code that did not work for me:
from openpyxl import load_workbook
from openpyxl.chart import BarChart, Series, Reference
wb = load_workbook(r'C:\Users\sma.xlsx')
sheet1 = wb['Output']
Chart2 = BarChart()
print(Chart2)
Chart2.style = 6
wb.save(r'r'C:\Users\sma.xlsx')
wb.close()
Can anyone give me the right approach to get my proper result?
Related
I am using pandas.to_excel() to generate xlsx files.
On my Win10 openpyxl is used by default because xlsxwriter is not installed. My question goes to both options.
The columns of resulting xlsx files are to small for the content. I have to adjust them via Excel.
This is how a generated sheet looks like
And this is how it looks like after auto-adjust the width in Excel itself.
I want that pandas/openpyxl/xlsxwriter does this auto-adjusting by itself. Is there an option or a way for this?
The only reliable way I know to do this is to use .autofit() in xlwings:
import pandas as pd
import xlwings as xw
filename = r"test.xlsx"
df = pd._testing.makeDataFrame()
df.to_excel(filename)
with xw.App(visible=False) as app:
wb = xw.Book(filename)
for ws in wb.sheets:
ws.autofit(axis="columns")
wb.save(filename)
wb.close()
I'm trying to build some charts using xlsxwriter as shown in the following code, but I also get to use openpyxl before reaching out this code. The error is:
AttributeError: 'Workbook' object has no attribute 'add_chart'
I'm understanding that it's trying to run this with openpyxl instead of xlsxwriter, is there a way to unistall openpxl before running this part? or can i force it to run with xlsxwriter? or anyother way?
workbook = xlsxwriter.Workbook(output_path + '\\' + input)
worksheet = workbook.add_worksheet()
workbook = writer.book
worksheet = writer.sheets['New_Freq']
chart = workbook.add_chart({'type': 'column'})
chart.add_series ({'values': '=New_Freq!$A:$B'})
worksheet.insert_chart('G2', chart)
I tried to insert a column into an excel.
However, the style of cells has been changed
CODE:
import openpyxl
wb = openpyxl.load_workbook('xt3.xlsx')
sheet = wb.worksheets[0]
sheet.insert_cols(0)
[enter image description here][1]wb.save("filename.xlsx")
https://i.stack.imgur.com/hl5QY.png
issues on bitbucket: https://bitbucket.org/openpyxl/openpyxl/issues/1098/bugs-insert_cols-changes-merge-cells-and
After some digging I wrote this code in openpyxl and xlrd/xlwt/xlutils.
Support both xls and xlsx.
Before
After
The key is to use copy and generate coordinate.
Code is here
This code will insert 3 columns; It keeps the background color but not all of the borders.
merged_cells_range = ws.merged_cells.ranges
for merged_cell in merged_cells_range:
merged_cell.shift(3,0)
ws.insert_cols(1,3)
I would like to change the background color of a chart, as in this example, using openpyxl.
In a google group discussion I found the following code snippet:
from openpyxl.chart.shapes import GraphicalProperties
props = GraphicalProperties(solidFill="999999")
chart.graphical_properties = props
chart.plot_area.graphical_properties = props
but it does not have any effect on the chart when saved to the excel file.
This functionality was seemingly broken in a previous version of openpyxl and is fixed as of release 2.4.7. To achieve the result as illustrated in your picture you need to change the solid fill color of the plot_area:
from openpyxl import Workbook
from openpyxl.chart import BarChart
from openpyxl.chart.shapes import GraphicalProperties
wb = Workbook()
ws = wb.active
chart = BarChart()
props = GraphicalProperties(solidFill="999999")
chart.plot_area.graphicalProperties = props
ws.add_chart(chart, "A1")
wb.save("bar.xlsx")
Please note: the member object holding the graphical properties of chart is chart.graphical_properties, whereas in plot_area it is named plot_area.graphicalProperties - which is itself an alias for plot_area.spPr.
You need to be sure to access the proper member to create a valid data structure that does look as you expect it to in the Excel file.
I am able to create vertical bar chart with below code:
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
for i in range(10):
ws.append([i])
from openpyxl.charts import BarChart, Reference, Series
values = Reference(ws, (1, 1), (10, 1))
series = Series(values, title="First series of values")
chart = BarChart()
chart.append(series)
ws.add_chart(chart)
wb.save("SampleChart.xlsx")
Can I create vertical bar chart with openpyxl? And export the same in image form?
The best chart support is currently in the excellent Xlsxwriter library. Improvements are coming to openpyxl, but as long as you only need to write files then Xlsxwriter is currently the best library.
As both libraries only generate the XML files, you cannot export the chart as an image. You might be able to this by remote control using xlwings but otherwise you should look at one of the visualisation libraries such as MatPlotLib.