我有一个使用 pptx 模块 python-pptx 生成并放置在 PPT 文件中的表。我想在我的 PPT 文件中表格的每一行之间添加一条线。
我当前的代码:
prs = Presentation('existing-presentation.pptx')
slide = prs.slides.add_slide(prs.slide_layouts[0])
title = slide.shapes.title
title.text = "Slide Title"
df_to_table(slide, df)
prs.save(output)
def df_to_table(slide, df, colnames=None):
rows, cols = df.shape
placeholder = slide.placeholders[10]
res = placeholder.insert_table(rows+1, cols)
res.table.columns[0].width = Pt(50)
if colnames is None:
colnames = list(df.columns)
# Insert the column names
for col_index, col_name in enumerate(colnames):
# Column names can be tuples
if not isinstance(col_name, str):
col_name = " ".join(col_name)
res.table.cell(0, col_index).text = col_name
res.table.cell(0, col_index).text_frame.paragraphs[0].font.size=Pt(8)
res.table.cell(0, col_index).text_frame.paragraphs[0].font.name='Arial'
res.table.cell(0, col_index).text_frame.paragraphs[0].font.color.rgb=RGBColor(0, 0, 0)
res.table.cell(0, col_index).fill.solid()
res.table.cell(0, col_index).fill.fore_color.rgb=RGBColor(255, 255, 255)
m = df.as_matrix()
for row in range(rows):
for col in range(cols):
val = m[row, col]
text = str(val)
res.table.cell(row + 1, col).text = text
res.table.cell(row + 1, col).text_frame.paragraphs[0].font.size=Pt(8)
res.table.cell(row + 1, col).text_frame.paragraphs[0].font.name='Arial'
res.table.cell(row + 1, col).text_frame.paragraphs[0].font.color.rgb=RGBColor(0, 0, 0)
res.table.cell(row + 1, col).fill.solid()
res.table.cell(row + 1, col).fill.fore_color.rgb=RGBColor(255, 255, 255)
res.table.rows[row+1].height = Pt(0.5)
if col == 0: #ignore first col with names
continue
else:
res.table.columns[col].width = Pt(55)