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 # 在文本框中寫入文字,文字內(nèi)容為每頁PPT第一行最后一列的數(shù)據(jù) 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行數(shù),cols列數(shù),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) # 按行寫入數(shù)據(jù),并且設置格式 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') |
|