Я хотел бы добавить заголовок (из списка titles
) к каждой из моих диаграмм в цикле, но цикл завершается сбоем, когда я пытаюсь добавить текстовое поле на диаграмму (с помощью итератора).Разрешены ли значения итераторов в качестве заголовков для диаграмм?Я использую Python 3.6.Есть идеи?
cleaned = [['AF In', 0.281777948141098], ['AF Top', 0.11941492557525635], ['AF View', 12.46446630278714], ['AF Non', 'AF V', 0.6745020151138306, 0.6817344427108765], ['AF Cab', 'AF N', 'AF HB', 0.6878681182861328, 0.2603790760040283, 0.05175277590751648]]
titles = ['first', 'second', 'third', 'fourth', 'fifth']
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.chart.data import XyChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches,Pt
from pptx.enum.chart import XL_LABEL_POSITION
from pptx.dml.color import RGBColor
from pptx.dml import fill
from pptx.chart.chart import ChartTitle
from pptx.chart.chart import Chart
# create presentation
prs = Presentation()
#For each list items from query_results (cleaned)
for idx, (chart_result, t) in enumerate(zip(cleaned, titles)):
#define size of chart
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
try:
#split the results into two arrays
arr1, arr2 = np.array_split(chart_result,2)
#create a table for chart data
chart_data = CategoryChartData()
#assign arr1 to categories
chart_data.categories = arr1
#assign arr2 to series
chart_data.add_series('series_1',(float(x) for x in arr2))
#add slide
prs.slides.add_slide(prs.slide_layouts[6])
#add chart to slide by index
prs.slides[idx].shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data)
#chart = shapes[0].chart
chart.chart_title.text_frame.text = t
except:
print(f'chart load failed {idx}')