python ppt表格样式展示
本篇文章是對另一篇文章的補充,好多情況下會遇到樣式的改變。在這里出現情況較多的時候可以封裝為函數,方便調用,復寫。
# -*- coding: utf-8 -*-
from pptx import Presentation
from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT
from pptx.chart.data import ChartData
from pptx.util import Pt
from pptx.dml.color import RGBColor
def write_cell(table, row, col, content, size, name,rgb):
? ? table.cell(row, col).text_frame.clear()
? ? new = table.cell(row, col).text_frame.paragraphs[0]
? ? new.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER
? ? new.text = content
? ? new.font.size = Pt(size)
? ? new.font.name = name
? ? new.font.color.rgb = rgb
def ppt(prs,template_path):
? ? # 表格
? ? table = [tb.table for tb in slide.shapes if tb.has_table][0]
? ? values = []
? ? for row in range(1,3):
? ? ? ? value = []
? ? ? ? for col in range(1,8):
? ? ? ? ? ? if type(sheet.cell(row, col).value) == type(1.1) and col== 7:
? ? ? ? ? ? ? ? s = str(round(sheet.cell(row, col).value * 100,2)) + '%'
? ? ? ? ? ? elif type(sheet.cell(row, col).value) == type(1.1) and col in [1,2,3,4,5,6]:
? ? ? ? ? ? ? ? s = str(round(sheet.cell(row, col).value))
? ? ? ? ? ? else :
? ? ? ? ? ? ? ? s = str(sheet.cell(row, col).value)
? ? ? ? ? ? value.append(s)
? ? ? ? values.append(value)
? ? for i in range(len(table.rows)):
? ? ? ? for j in range(len(table.columns)):
? ? ? ? ? ? if ?values[i][j].startswith('-'):
? ? ? ? ? ? ? ? write_cell(table, i, j, str(values[i][j]), 18, "宋體",RGBColor(255, 0, 0))
? ? ? ? ? ? else: ? ?
? ? ? ? ? ? ? ? write_cell(table, i, j, str(values[i][j]), 14, "宋體",RGBColor(0, 0, 0))
總結
以上是生活随笔為你收集整理的python ppt表格样式展示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [bzoj1050 HAOI2006]
- 下一篇: 写给深圳首期Python自动化开发周未班