Home > Enterprise >  i'm trying to add the pichart into the excel file
i'm trying to add the pichart into the excel file

Time:04-20

i'm trying to add the piechart to an excel sheet and i can't seem to understand what i've done wrong

  game_name = ['Rocket League','FIFA 19','FIFA 18', 'FIFA 17',  'FIFA Online 3','FIFA 20' ,'Madden NFL 2017','NBA 2K18','FIFA Online 4']
    money=['9171818.72 ', '3242642.34 ', '2233697.69', '2233697.69', '1482612.63',' 1434341.90', '1144673.72 ', ' 1004000.00','1000000.00']

explode=(0.1,0,0,0,0,0,0,0,0)
plt.style.use('ggplot')
plt.title('les jeux ayant le plus de gain')
fig3= plt.pie(x=money, explode=explode, labels=game_name, shadow=True, startangle=90,autopct='%.2f%%')
plt.axis('equal')  
plt.legend(loc='upper left')
plt.show()
#plt.savefig('piechart.png')
sht.pictures.add(
    fig3,
    left=sht.range("A103").left,
    top= sht.range("A103").top,
    height=300,
    width=500,
)

i'm gettiing this error "don't know what to do with that image object"

CodePudding user response:

use .insert_image()

import xlsxwriter
from matplotlib import pyplot as plt


game_name = ['Rocket League','FIFA 19','FIFA 18', 'FIFA 17',  'FIFA Online 3','FIFA 20' ,'Madden NFL 2017','NBA 2K18','FIFA Online 4']
money=['9171818.72 ', '3242642.34 ', '2233697.69', '2233697.69', '1482612.63',' 1434341.90', '1144673.72 ', ' 1004000.00','1000000.00']

explode=(0.1,0,0,0,0,0,0,0,0)
plt.style.use('ggplot')
plt.title('les jeux ayant le plus de gain')
fig3= plt.pie(x=money, explode=explode, labels=game_name, shadow=True, startangle=90,autopct='%.2f%%')
plt.axis('equal')  
plt.legend(loc='upper left')
plt.savefig('piechart.png')

# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('images.xlsx')
sht = workbook.add_worksheet()

sht.insert_image('A103', 'piechart.png')
workbook.close()

CodePudding user response:

If you want to add a pie chart to Excel you could do it directly using XlsxWriter, rather than adding an image. Something like this:

import xlsxwriter

workbook = xlsxwriter.Workbook('chart_pie.xlsx')
worksheet = workbook.add_worksheet()

bold = workbook.add_format({'bold': 1})
money_format = workbook.add_format({'num_format': '$#,##0'})

# Add the worksheet data that the charts will refer to.
headings = ['Game', 'Earnings']

game_name = ['Rocket League','FIFA 19','FIFA 18', 'FIFA 17', 'FIFA Online 3',
             'FIFA 20' ,'Madden NFL 2017', 'NBA 2K18', 'FIFA Online 4']
money=[9171818.72 , 3242642.34 , 2233697.69, 2233697.69, 1482612.63,
       1434341.90, 1144673.72, 1004000.00, 1000000.00]

worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', game_name)
worksheet.write_column('B2', money)

# Create a new chart object.
chart = workbook.add_chart({'type': 'pie'})

# Configure the series.
max_row = len(game_name)
chart.add_series({
    'categories': ['Sheet1', 1, 0, max_row, 0],
    'values':     ['Sheet1', 1, 1, max_row, 1],
})

# Add a title.
chart.set_title({'name': 'Les jeux ayant le plus de gain'})

# Set an Excel chart style. Colors with white outline and shadow.
chart.set_style(10)

# Insert the chart into the worksheet.
worksheet.insert_chart('D2', chart)

# Set the first column width for clarity.
worksheet.set_column(0, 0, 15)

# Set the second column witch and format for currency.
worksheet.set_column(1, 1, 10, money_format)

workbook.close()

Output:

enter image description here

  • Related