首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:更改表格样式或向单元格添加边框。

Python:更改表格样式或向单元格添加边框。
EN

Stack Overflow用户
提问于 2017-03-05 16:23:41
回答 3查看 10.1K关注 0票数 6

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

代码语言:javascript
复制
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')
EN

Stack Overflow用户

发布于 2022-08-10 22:10:02

我很难弄明白为什么这不管用。对于其他努力解决这个问题的人,我不得不在函数的末尾添加以下内容:

代码语言:javascript
复制
return cell

在使用时,您希望以如下方式使用该函数:

代码语言:javascript
复制
cell = _set_cell_border(cell)
票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42610829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档