效果:
数据替换前 : 替换数据后:
实现代码
import collections.abc
from pptx import Presentation
from pptx.util import Cm,Pt
import pyodbc
import pandas as pd
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_TICK_LABEL_POSITION
import datetime
from dateutil.relativedelta import relativedelta
from pptx.dml.color import RGBColor
import os
from openpyxl import load_workbook
import logging
import matplotlib as plt
import datetime
from datetime import timedelta
import calendar
from lxml import etree
from pptx.oxml.ns import qn
import numpy as np
from pptx.enum.text import PP_ALIGN
plt.rcParams ['font.sans-serif']= ['Microsoft YaHei']
import datetime,calendar
import warnings
warnings.filterwarnings('ignore')path='E:\\13 python 下侧双x轴折线图\\'
prs = Presentation(path+'ppt图形数据替换.pptx')
data_time=pd.read_excel(path+"数据.xlsx",sheet_name='sheet1')ppt_8=prs.slides[8].shapes #获取ppt第8页
chart_data_8=CategoryChartData()
ser1=data_time.iloc[[38,37,39],2] # 取数据
ser2=data_time.iloc[[38,37,39],3]
# print(route_tk)x=['第一季度','第二季度','第三季度']
chart_data_8.categories=x
chart_data_8.add_series(name='系列一',values=ser1,number_format='0.0')
chart_data_8.add_series(name='系列二',values=ser2,number_format='0.0')
group_1=ppt_8[0]
chart_1=group_1
catrgory_axis_1=chart_1.chart.category_axis
catrgory_axis_1.tick_label_position=XL_TICK_LABEL_POSITION.LOW
catrgory_axis_1.tick_labels.font.size=Pt(14)
chart_1.chart.replace_data(chart_data=chart_data_8)prs.save(path+'replace_chartdata_bar0528.pptx')