⽤python给表格加边框_Python-PPTX:更改表格样式或向单
元格添加边框
我已经开始整理⼀些代码来获取Pandas数据并将其放⼊PowerPoint幻灯⽚中.我使⽤的模板默认为中等样式2 – 重⾳1,这很好,因为更改字体和背景相当容易,但似乎没有实现python-pptx的部分允许更改单元格边框.以下是我的代码,可以解决任何问题. (改变XML或更改模板默认设置以填充更好的样式对我来说是个不错的选择,但是没有到关于如何做的好⽂档).中等风格4对我来说是理想的,因为它具有我正在寻的边界.
import pandas
import numpy
from pptx import Presentation
from pptx.util import Inches, Pt
from lor import RGBColor
#Template Location
table设置内边框
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 = lumns.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
= "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
#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:
#Write rest of table entries
else:
#Write Table to File
prs.save('C:/Desktop/test.pptx')
解决⽅法:
也许不是真正⼲净的代码,但允许我调整表格中所有单元格的所有边框:
l.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')
标签:python,border,powerpoint,python-pptx

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。