打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python批量将excel中数据写入PPT
userphoto

2022.12.14 广东

关注
  •  
from pptx import Presentationfrom pptx.util import Cm, Pt, Inchesimport numpy as npimport pandas as pdfrom pptx.enum.text import PP_ALIGNfrom pptx.enum.text import MSO_ANCHORfrom pptx.dml.color import RGBColor# 批量新建PPTprs = Presentation()blank_slide = prs.slide_layouts[6]for n in range(0,532):
    slide = prs.slides.add_slide(blank_slide)
   
    # 在指定位置添加文本框
    textbox = slide.shapes.add_textbox(left = Cm(0.2),
                             top = Cm(0),
                             width = Cm(20),
                             height =  Cm(1))
    tf = textbox.text_frame    # 在文本框中写入文字,文字内容为每页PPT第一行最后一列的数据
    textbox.text = str(da.iloc[5*n,20])
    tf.paragraphs[0].font.size = Pt(15)
    tf.paragraphs[0].font.name = '微软雅黑'
    tf.paragraphs[0].font.bold = True

        
    # 添加表格:rows行数,cols列数,left和top是在PPT中的位置,width是表的列宽,height是表的行高
    table = slide.shapes.add_table(rows = 6,
                                 cols = 20,
                                 left = Cm(0.2),
                                 top = Cm(1.0),
                                 width = Cm(25),
                                 height =  Cm(1)
                            )
    table = table.table    # 写入表头,设置表头的格式
    header = da.columns[:-1]
    for i, h in enumerate(header):
        cell = table.cell(0, i)
        cell.text = h
        cell.text_frame.paragraphs[0].font.size = Pt(9)
        cell.text_frame.paragraphs[0].font.color.rgb = RGBColor(255, 255, 255)
        cell.text_frame.paragraphs[0].font.name = '微软雅黑'
        cell.text_frame.paragraphs[0].font.bold = True
        cell.text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER
        cell.vertical_anchor = MSO_ANCHOR.MIDDLE
        cell.fill.solid()
        cell.fill.fore_color.rgb = RGBColor(49, 134, 155)

    # 按行写入数据,并且设置格式
    r = 5
    c = da.shape[1]
    for i in range(r):
        for j in range(c-1):
            cell = table.cell(i+1, j)
            cell.text = str(da.iloc[i+5*n,j])
            cell.text_frame.paragraphs[0].font.size = Pt(9)
            cell.text_frame.paragraphs[0].font.name = '微软雅黑'
            cell.text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER
            cell.vertical_anchor = MSO_ANCHOR.MIDDLE
            cell.fill.solid()
            cell.fill.fore_color.rgb = RGBColor(240, 240, 240)

        prs.save('test.pptx')
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
新书上市 | Python办公自动化(好友新书,值得一看,文末有福利)
文末福利|使用Python转换PDF,Word/Excel/PPT/md/HTML都能转!
VBA 会被 Python 代替吗?
折腾一天
Excel办公自动化,拆分工作表,python两行代码1秒钟
Python使用openpyxl模块读写excel文件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服