分享

python批量将excel中数据写入PPT

 昵称163835 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')

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多