6

我已经开始整理一些代码来获取 Pandas 数据并将其放入 PowerPoint 幻灯片中。我使用的模板默认为 Medium Style 2 - Accent 1,这很好,因为更改字体和背景相当容易,但 python-pptx 似乎没有实现部分允许更改单元格边框。下面是我的代码,对任何解决方案都开放。(更改 XML 或更改模板默认值以填充更好的样式对我来说是不错的选择,但还没有找到关于如何做的好的文档)。中型 4 对我来说是理想的,因为它具有我正在寻找的边界。

import pandas
import numpy
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor

#Template Location
tmplLoc = 'C:/Desktop/'

#Read in Template
prs = Presentation(tmplLoc+'Template.pptx')

#Import data as Pandas Dataframe - dummy data for now
df = pandas.DataFrame(numpy.random.randn(10,10),columns=list('ABCDEFGHIJ'))

#Determine Table Header
header = list(df.columns.values)

#Determine rows and columns
in_rows = df.shape[0]
in_cols = df.shape[1]

#Insert table from C1 template
slide_layout = prs.slide_layouts[11]
slide = prs.slides.add_slide(slide_layout)

#Set slide title
title_placeholder = slide.shapes.title
title_placeholder.text = "Slide Title"

#Augment placeholder to be a table
placeholder = slide.placeholders[1]
graphic_frame = placeholder.insert_table(rows = in_rows+1, cols = in_cols)
table = graphic_frame.table
#table.apply_style = 'MediumStyle4'
#table.apply_style = 'D7AC3CCA-C797-4891-BE02-D94E43425B78'

#Set column widths
table.columns[0].width = Inches(2.23)
table.columns[1].width = Inches(0.9)
table.columns[2].width = Inches(0.6)
table.columns[3].width = Inches(2)
table.columns[4].width = Inches(0.6)
table.columns[5].width = Inches(0.6)
table.columns[6].width = Inches(0.6)
table.columns[7].width = Inches(0.6)
table.columns[8].width = Inches(0.6)
table.columns[9].width = Inches(0.6)
#total_width = 2.23+0.9+0.6+2+0.6*6

#Insert data into table
for rows in xrange(in_rows+1):
    for cols in xrange(in_cols):
        #Write column titles
        if rows == 0:
            table.cell(rows, cols).text = header[cols]
            table.cell(rows, cols).text_frame.paragraphs[0].font.size=Pt(14)
            table.cell(rows, cols).text_frame.paragraphs[0].font.color.rgb = RGBColor(255, 255, 255)
            table.cell(rows, cols).fill.solid()
            table.cell(rows, cols).fill.fore_color.rgb=RGBColor(0, 58, 111) 
        #Write rest of table entries
        else:
            table.cell(rows, cols).text = str("{0:.2f}".format(df.iloc[rows-1,cols]))
            table.cell(rows, cols).text_frame.paragraphs[0].font.size=Pt(10)
            table.cell(rows, cols).text_frame.paragraphs[0].font.color.rgb = RGBColor(0, 0, 0)
            table.cell(rows, cols).fill.solid()
            table.cell(rows, cols).fill.fore_color.rgb=RGBColor(255, 255, 255)

#Write Table to File
prs.save('C:/Desktop/test.pptx')
4

2 回答 2

9

也许不是真正干净的代码,但允许我调整表格中所有单元格的所有边框:

from pptx.oxml.xmlchemy import OxmlElement

def SubElement(parent, tagname, **kwargs):
        element = OxmlElement(tagname)
        element.attrib.update(kwargs)
        parent.append(element)
        return element

def _set_cell_border(cell, border_color="000000", border_width='12700'):
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()
    for lines in ['a:lnL','a:lnR','a:lnT','a:lnB']:
        ln = SubElement(tcPr, lines, w=border_width, cap='flat', cmpd='sng', algn='ctr')
        solidFill = SubElement(ln, 'a:solidFill')
        srgbClr = SubElement(solidFill, 'a:srgbClr', val=border_color)
        prstDash = SubElement(ln, 'a:prstDash', val='solid')
        round_ = SubElement(ln, 'a:round')
        headEnd = SubElement(ln, 'a:headEnd', type='none', w='med', len='med')
        tailEnd = SubElement(ln, 'a:tailEnd', type='none', w='med', len='med')

基于这篇文章:https ://groups.google.com/forum/#!topic/python-pptx/UTkdemIZICw

于 2018-01-17T07:11:56.450 回答
1

如果其他人再次遇到此问题,应对JuuLes87发布的解决方案进行一些更改,以避免 Microsoft Office PowerPoint 需要修复生成的演示文稿。

仔细查看了pptx生成的表的xml字符串,发现需要修复表示似乎是因为'a:lnL' or 'a:lnR' or 'a:lnT' or '的重复节点'a:tcPr' 的子元素中的 a:lnB'。所以我们只需要在插入这些节点之前删除 ['a:lnL','a:lnR','a:lnT','a:lnB'] 的节点,如下所示。

from pptx.oxml.xmlchemy import OxmlElement

def SubElement(parent, tagname, **kwargs):
    element = OxmlElement(tagname)
    element.attrib.update(kwargs)
    parent.append(element)
    return element

def _set_cell_border(cell, border_color="000000", border_width='12700'):
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()
    for lines in ['a:lnL','a:lnR','a:lnT','a:lnB']:
        
        # Every time before a node is inserted, the nodes with the same tag should be removed.
        tag = lines.split(":")[-1]
        for e in tcPr.getchildren():
            if tag in str(e.tag):
                tcPr.remove(e)
        # end

        ln = SubElement(tcPr, lines, w=border_width, cap='flat', cmpd='sng', algn='ctr')
        solidFill = SubElement(ln, 'a:solidFill')
        srgbClr = SubElement(solidFill, 'a:srgbClr', val=border_color)
        prstDash = SubElement(ln, 'a:prstDash', val='solid')
        round_ = SubElement(ln, 'a:round')
        headEnd = SubElement(ln, 'a:headEnd', type='none', w='med', len='med')
        tailEnd = SubElement(ln, 'a:tailEnd', type='none', w='med', len='med')
于 2021-08-24T11:53:04.070 回答