import json
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
from pptx.util import Cm, Pt,Inches
from pptx.enum.text import PP_ALIGN
from pptx.dml.color import RGBColor
from pptx_tools import utils
'''
#提取文本
print('\n' + '---------提取文本--------'+'\n')
print(shapes)
for shape in shapes:
if hasattr(shape, "text"):
print(shape)
print(shape.text.encode('utf-8').strip().decode())
'''
#提取表格
def set_table():
print('\n'+'---------提取表格--------')
for slide in prs.slides:
for shape in slide.shapes:
#print(shape.shape_type)
if shape.shape_type == MSO_SHAPE_TYPE.TABLE:
print('table--')
# print(shape.shape_type)
_table = shape.table
if len(_table.columns) <= 1:
continue
print(type(_table))
#print('行数 s% ,列数 %s' %(_table.rows,30))
print('行数' + str(len(_table.rows)))
print('列数' + str(len(_table.columns)))
for row_ct,row in enumerate(_table.rows):
for j in range(len(_table.columns)):
#_tx = row.cells[j].text_frame.text.strip()
#print(_tx)
if row_ct >= 2 and j>=2:
row.cells[j].text_frame.text = str(j)
para = row.cells[j].text_frame.paragraphs[0]
para.font.size=Pt(18) #
para.font.name = '微软雅黑'
para.alignment = PP_ALIGN.CENTER #居中
para.font.color.rgb = RGBColor(0, 0, 0)
row.cells[j].fill.solid()
row.cells[j].fill.fore_color.rgb=RGBColor(255, 255, 255)
'''
#表格内文本样式
para = row.cells[j].text_frame.add_paragraph()
para.text = str(j)
para.font.size = Pt(16)
para.font.name = '微软雅黑'
para.alignment = PP_ALIGN.CENTER
#单元格背景填充
cell = row.cells[j]
cell.fill.solid()
cell.fill.fore_color.rgb = RGBColor(255, 255, 255)
'''
'''
cell = table.cell(i+1, a)
tf = row.cells[j].text_frame
para = tf.add_paragraph()
para.alignment = PP_ALIGN.CENTER #居中
row.cells[j].fill.solid()
row.cells[j].fill.fore_color.rgb = RGBColor(255, 255, 255)
'''
#other_cell = _table.cell(2, 13) # x,y 需调整样式的单元格坐标
#para = other_cell.text_frame.add_paragraph()
#para.text = text
#para.font.size = Pt(16) # 字体大小
#para.font.name = '微软雅黑'
#para.alignment = PP_ALIGN.CENTER # 对齐,此处为居中
#para.font.bold = Ture # 字体加粗
#para.font.color.rgb = RGBColor(255, 0, 0) # 字体颜色
def set_text():
print('\n'+'---------提取文本--------')
for slide in prs.slides:
for shape in slide.shapes:
#print(shape.shape_type)
if shape.shape_type == MSO_SHAPE_TYPE.TEXT_BOX:
print('text...' + '\n')
print(shape.text)
if 'XX月XX日' in shape.text.encode('utf-8').strip().decode():
shape.text = '(1月10日)'
para = shape.text_frame.paragraphs[0]
para.font.size=Pt(26) #
para.font.name = '微软雅黑'
para.font.color.rgb = RGBColor(255, 255, 0)
para.alignment = PP_ALIGN.CENTER #居中
prs = Presentation('report.pptx')
shapes = prs.slides[0].shapes
set_text()
set_table()
prs.save('report3.pptx')
pptfile = r'D:\pythonCode\ppt_wirte\report3.pptx'
png_folder = r'D:\pythonCode\ppt_wirte\temp'
utils.save_pptx_as_png(png_folder, pptfile, overwrite_folder=True)