Erstellen Sie ein Diagramm in PPT mit Python aus Excel-DatenPython

Python-Programme
Anonymous
 Erstellen Sie ein Diagramm in PPT mit Python aus Excel-Daten

Post by Anonymous »

Ich möchte Daten aus zwei Excel-Dateien nehmen und daraus ein einzelnes Diagramm erstellen und das Diagramm als interaktives Diagramm in eine Kopie einer PPT-Vorlagenfolie mit Python einfügen. Ich verwende einen Datenrahmen zum Speichern der Daten.
Ich habe dies versucht -

Code: Select all

import pptx
from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
import pandas as pd
excelfile1 = pd.read_excel("1st.xlsx",sheet_name="sheet1")
excelfile2 = pd.read_excel("2nd.xlsx",sheet_name="sheet1")
df1 = excelfile1.iloc[177:183,20:23]
df2 = excelfile2.iloc[177:183,21:23]
df1.columns = ["Range","PDF (%)","CDF (%)"]
df2.columns = ["PDF (%)","CDF (%)"]
df = pd.concat([df1,df2],axis=1)
print(df)
# create presentation with 1 slide ------
prs = Presentation("Template.pptx")
slide = prs.slides[10]

# define chart data ---------------------
chart_data = CategoryChartData()
chart_data.categories = df1["Range"]
chart_data.add_series('PDF %', df1["PDF (%)"])

# add chart to slide --------------------
x, y, cx, cy = Inches(1), Inches(1), Inches(6), Inches(4.5)
cchart = slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
)
# chart_data1 = CategoryChartData()
# chart_data1.categories = df1["Range"]
chart_data.add_series('CDF %', df1["CDF (%)"])

# add chart to slide --------------------
x, y, cx, cy = Inches(1), Inches(1), Inches(6), Inches(4.5)
slide.shapes.add_chart(
XL_CHART_TYPE.LINE, x, y, cx, cy, chart_data
)
prs.save('chart-01.pptx')
Wenn ich diesen Code jedoch ausführe, erhalte ich dies als Ausgabe -
Ausgabe vom Code
Können Sie mir bitte erklären, wo ich falsch liege?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post