def CreateExcel(request):
# Create workbook
wb = Workbook()
ws = wb.active
my_fruits = ['apple', 'apple', 'orange', 'grape', 'apple', 'apple', 'grape', 'apple', 'apple', 'apple', 'orange', 'orange', 'grape', 'orange', 'apple',
'apple', 'apple', 'apple', 'orange', 'apple', 'peach', 'mango', 'kiwi', 'kiwi', 'mango', 'kiwi', 'banana', 'pear', 'tangerine',
'pomegranate', 'raspberry', 'blueberry', 'blueberry', 'banana', 'blueberry', 'blackberry', 'watermelon']
fruit_set = set(my_fruits)
fruit_categories = []
for fruit in fruit_set:
fruit_categories.append(str(fruit))
ws['A1'] = "fruit"
for i in range(2, len(my_fruits)+1):
ws[(chr(0 + ord('A'))+str(i))] = my_fruits[i-1]
ws['B1'] = "Fruit"
ws['C1'] = "Quantity"
fruit_count_dict = {}
for fruit in fruit_categories:
fruit_count_dict[str(fruit)] = 0
for r in my_fruits:
fruit_count_dict[r] += 1
sorted_fruit_count = sorted(fruit_count_dict.items(), key=operator.itemgetter(1))
print (sorted_fruit_count)
row = 2
for j in sorted_fruit_count:
ws[(chr(0 + ord('B'))+str(row))] = str(j[0])
ws[(chr(0 + ord('C'))+str(row))] = j[1]
row += 1
# Bar chart of fruits
chart1 = BarChart()
chart1.type = "bar"
chart1.style = 10
chart1.title = "A List of Fruits and their Quantities"
chart1.legend = None
chart1.shape = 4
data = Reference(ws, min_col=3, min_row=1, max_row=len(fruit_categories)+1)
cats = Reference(ws, min_col=2, min_row=2, max_row=len(fruit_categories)+1)
chart1.add_data(data, titles_from_data=True)
chart1.set_categories(cats)
chart1.height = 10
chart1.width = 20
# set a pattern for the whole data set
series = chart1.series[0]
fill = PatternFillProperties(prst="pct5")
fill.foreground = ColorChoice(prstClr="navy")
fill.background = ColorChoice(prstClr="navy")
series.graphicalProperties.pattFill = fill
# set a pattern for a 6th data point (0-indexed)
pt = DataPoint(idx=5)
pt.graphicalProperties.pattFill = PatternFillProperties(fgClr=ColorChoice(prstClr='gold'), bgClr=ColorChoice(prstClr='gold'))
series.dPt.append(pt)
ws.add_chart(chart1, "E30")
# Pie chart
# Use same data as bar chart
pie = PieChart()
pie.add_data(data, titles_from_data=True)
pie.set_categories(cats)
pie.title = "Fruits by category - % labels"
pie.height = 15
pie.width = 15
# Find index of desired slice to explode
apple_slice_index = 0
orange_slice_index = 0
for f in range(0, len(sorted_fruit_count)):
if sorted_fruit_count[f][0] == "apple":
apple_slice_index = f
elif sorted_fruit_count[f][0] == "orange":
orange_slice_index = f
# Cut the desired slice out of the pie
slice = DataPoint(idx=apple_slice_index, explosion=10)
pie.series[0].data_points = [slice]
# set a pattern for the whole data set
pieseries = pie.series[0]
fill = PatternFillProperties(prst="pct5")
fill.foreground = ColorChoice(prstClr="navy")
fill.background = ColorChoice(prstClr="navy")
pieseries.graphicalProperties.pattFill = fill
# set a pattern for a 6th data point (0-indexed)
pt = DataPoint(idx=apple_slice_index)
pt.graphicalProperties.pattFill = PatternFillProperties(fgClr=ColorChoice(prstClr='blue'), bgClr=ColorChoice(prstClr='blue'))
pieseries.dPt.append(pt)
# set a pattern for a 6th data point (0-indexed)
pt = DataPoint(idx=orange_slice_index)
pt.graphicalProperties.pattFill = PatternFillProperties(fgClr=ColorChoice(prstClr='forestGreen'), bgClr=ColorChoice(prstClr='forestGreen'))
pieseries.dPt.append(pt)
ws.add_chart(pie, "E1")
pie.dataLabels = DataLabelList()
pie.dataLabels.showPercent = True
name_part = 'fruit'
# prep the Excel file
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="'+ name_part +'.xls"'
# Save the workbook
wb.save(response)
return response
--
You received this message because you are subscribed to the Google Groups "openpyxl-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openpyxl-user...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/openpyxl-users/8c715eed-c1f2-44cc-8a98-7e52fe26125cn%40googlegroups.com.