如何检测 pptx 幻灯片中所有现有的 PNG/JPG 图像,然后使用 python pptx 处理它们的 size/layout?
How to detect all existing PNG/JPG images in pptx slide and then manipulate their size/layout using python pptx?
我有一个 PowerPoint 演示文稿,其中包含数十张仅标题格式但带有图像的幻灯片 objects。 objects 通常是 (1) 一个大图形图像,(2) 一个小图例图像,以及 (3) 另一个小图例图像。图像通常过大,超过幻灯片尺寸或相互重叠。我想使用以下步骤清理幻灯片图像:
- 打开已知文件名的现有 pptx (已经知道如何操作)
- 从幻灯片 2 开始
- 查找幻灯片中的所有图像。确定哪个是最左边的图像。 (我卡在这里了。)
- 最左边的图像是 (1) 图形图像。调整它直到达到 6.4" 的高度或 13" 的宽度。将其与最左边缘和最上边缘对齐(触摸幻灯片标题文本框)
- 如果图表图像的高度为 6.4",请将图例图像移动到图表图像的右边缘附近。对齐它们的上边缘。
- 如果图形图像的宽度为 13",请将图例图像移动到图形图像的底部边缘附近。对齐它们的右边缘。
- 重复直到最后一张幻灯片
- 跳过第 header 部分布局的幻灯片。
更新:到目前为止,这是我的代码
from pptx import Presentation
import glob
from pptx.util import Inches
from pptx.enum.text import PP_ALIGN
f = open('C:\Users\7188712\Desktop\Script Validation\XTI\Blade2T_XTI (Tag1.7.1.40).pptx', 'rb')
prs = Presentation(f)
slides = prs.slides
for slide in prs.slides:
print('\nslide number ', slides.index(slide)+1, slide.shapes.title.text)
for shape in slide.shapes:
#if shape.shape_type == 13: #Type 13 means PICTURE type
#print("id: %s, height: %s, width: %s, ext: %s"% \
# (shape.shape_id, round(shape.height.inches,2), round(shape.width.inches,2), shape.image.ext))
print('id: %s, height: %s, width: %s'% \
(shape.shape_id, round(shape.height.inches,2), round(shape.width.inches,2)), shape.shape_type, shape.name)
if shape.name == 'Title 1':
shape.alignment = PP_ALIGN.LEFT
终于!
from pptx import Presentation
import glob
from pptx.util import Inches, Emu, Pt
from pptx.enum.text import PP_ALIGN
#Initialize variables
pic_start = 7 #Slide page where JMP graphs start
space_ht = Inches(6.3) #Space for images height
space_wt = Inches(13) #Space for images width
path = 'C:\Users\Name\Desktop\Folder\'
fname = 'filename'
#Open pptx
f = open(path+fname, 'rb')
prs = Presentation(f)
slides = prs.slides
#Scan each slide
for slide in prs.slides:
print('\nslide number ', slides.index(slide)+1, slide.shapes.title.text)
for shape in slide.shapes:
#Show all objects in the slide
print('id: %s, height: %s, width: %s, left: %s'%(shape.shape_id, round(shape.height.inches,2), round(shape.width.inches,2), \
round(shape.left.inches,2)), shape.shape_type, shape.name)
#Modify slide sections to section header-like format
if shape.shape_type == 17 and shape.text == '<change layout for title slide>': #Type 17 means TEXT_BOX type
shape.text = ''
slide.shapes[0].width = Inches(11.5)
slide.shapes[0].height = Inches(3.12)
slide.shapes[0].left = Inches(0.91)
slide.shapes[0].top = Inches(1.87)
slide.shapes[0].text_frame.fit_text(font_family=u'Verdana', max_size=60, bold=True)
#Modify slide titles starting page 2
if shape.shape_type == 14 and slides.index(slide) != 0: #Type 14 means PLACEHOLDER type and we don't edit the title page
if shape.text == slide.shapes.title.text: #check if this is the title
shape.height = Inches(0.52)
shape.text_frame.paragraphs[0].alignment = PP_ALIGN.LEFT
shape.text_frame.fit_text(font_family=u'Verdana', max_size=24, bold=True)
shape.left = Inches(0.2)
top_margin = shape.height.inches
#Modify pages starting picstart for picture
if shape.shape_type == 13 and slides.index(slide) > pic_start-2 and slide.shapes.title.text != 'Legends': #Type 13 means PICTURE type
shape.left = Inches(0.2)
shape.top = Inches(top_margin)
pic_ratio = shape.width/shape.height
space_ratio = space_wt/space_ht
#Adjust length and with to fit slide
if pic_ratio > space_ratio:
shape.width = Emu(space_wt)
shape.height = Emu(space_wt/pic_ratio)
else:
shape.height = Emu(space_ht)
shape.width = Emu(space_ht*pic_ratio)
#Save and close
f.close()
prs.save(path+fname)
我有一个 PowerPoint 演示文稿,其中包含数十张仅标题格式但带有图像的幻灯片 objects。 objects 通常是 (1) 一个大图形图像,(2) 一个小图例图像,以及 (3) 另一个小图例图像。图像通常过大,超过幻灯片尺寸或相互重叠。我想使用以下步骤清理幻灯片图像:
- 打开已知文件名的现有 pptx (已经知道如何操作)
- 从幻灯片 2 开始
- 查找幻灯片中的所有图像。确定哪个是最左边的图像。 (我卡在这里了。)
- 最左边的图像是 (1) 图形图像。调整它直到达到 6.4" 的高度或 13" 的宽度。将其与最左边缘和最上边缘对齐(触摸幻灯片标题文本框)
- 如果图表图像的高度为 6.4",请将图例图像移动到图表图像的右边缘附近。对齐它们的上边缘。
- 如果图形图像的宽度为 13",请将图例图像移动到图形图像的底部边缘附近。对齐它们的右边缘。
- 重复直到最后一张幻灯片
- 跳过第 header 部分布局的幻灯片。
更新:到目前为止,这是我的代码
from pptx import Presentation
import glob
from pptx.util import Inches
from pptx.enum.text import PP_ALIGN
f = open('C:\Users\7188712\Desktop\Script Validation\XTI\Blade2T_XTI (Tag1.7.1.40).pptx', 'rb')
prs = Presentation(f)
slides = prs.slides
for slide in prs.slides:
print('\nslide number ', slides.index(slide)+1, slide.shapes.title.text)
for shape in slide.shapes:
#if shape.shape_type == 13: #Type 13 means PICTURE type
#print("id: %s, height: %s, width: %s, ext: %s"% \
# (shape.shape_id, round(shape.height.inches,2), round(shape.width.inches,2), shape.image.ext))
print('id: %s, height: %s, width: %s'% \
(shape.shape_id, round(shape.height.inches,2), round(shape.width.inches,2)), shape.shape_type, shape.name)
if shape.name == 'Title 1':
shape.alignment = PP_ALIGN.LEFT
终于!
from pptx import Presentation
import glob
from pptx.util import Inches, Emu, Pt
from pptx.enum.text import PP_ALIGN
#Initialize variables
pic_start = 7 #Slide page where JMP graphs start
space_ht = Inches(6.3) #Space for images height
space_wt = Inches(13) #Space for images width
path = 'C:\Users\Name\Desktop\Folder\'
fname = 'filename'
#Open pptx
f = open(path+fname, 'rb')
prs = Presentation(f)
slides = prs.slides
#Scan each slide
for slide in prs.slides:
print('\nslide number ', slides.index(slide)+1, slide.shapes.title.text)
for shape in slide.shapes:
#Show all objects in the slide
print('id: %s, height: %s, width: %s, left: %s'%(shape.shape_id, round(shape.height.inches,2), round(shape.width.inches,2), \
round(shape.left.inches,2)), shape.shape_type, shape.name)
#Modify slide sections to section header-like format
if shape.shape_type == 17 and shape.text == '<change layout for title slide>': #Type 17 means TEXT_BOX type
shape.text = ''
slide.shapes[0].width = Inches(11.5)
slide.shapes[0].height = Inches(3.12)
slide.shapes[0].left = Inches(0.91)
slide.shapes[0].top = Inches(1.87)
slide.shapes[0].text_frame.fit_text(font_family=u'Verdana', max_size=60, bold=True)
#Modify slide titles starting page 2
if shape.shape_type == 14 and slides.index(slide) != 0: #Type 14 means PLACEHOLDER type and we don't edit the title page
if shape.text == slide.shapes.title.text: #check if this is the title
shape.height = Inches(0.52)
shape.text_frame.paragraphs[0].alignment = PP_ALIGN.LEFT
shape.text_frame.fit_text(font_family=u'Verdana', max_size=24, bold=True)
shape.left = Inches(0.2)
top_margin = shape.height.inches
#Modify pages starting picstart for picture
if shape.shape_type == 13 and slides.index(slide) > pic_start-2 and slide.shapes.title.text != 'Legends': #Type 13 means PICTURE type
shape.left = Inches(0.2)
shape.top = Inches(top_margin)
pic_ratio = shape.width/shape.height
space_ratio = space_wt/space_ht
#Adjust length and with to fit slide
if pic_ratio > space_ratio:
shape.width = Emu(space_wt)
shape.height = Emu(space_wt/pic_ratio)
else:
shape.height = Emu(space_ht)
shape.width = Emu(space_ht*pic_ratio)
#Save and close
f.close()
prs.save(path+fname)