本文整理汇总了Python中pptx.util.Inches方法的典型用法代码示例。如果您正苦于以下问题:Python util.Inches方法的具体用法?Python util.Inches怎么用?Python util.Inches使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pptx.util
的用法示例。
在下文中一共展示了util.Inches方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pic_to_ppt
# 需要导入模块: from pptx import util [as 别名]
# 或者: from pptx.util import Inches [as 别名]
def pic_to_ppt(filename): #前提是图片文件名全为数字,否则还需要修改
if not os.path.exists(filename):
os.mkdir(filename)
ppt = pptx.Presentation()
pic_path=[]
for i in os.walk(filename).__next__()[2]:
if i.endswith('.png'):
pic_path.append(i)
#若是不全为数字,则可尝试运行下列代码
# ls=[]
# for png in pic_path:
# s=''
# for item in png:
# if item<='9' and item>='0':
# s+=item
# ls.append(s+'.png')
# pic_path=ls
pic_path.sort(key=lambda item:int(item.split('.')[0]))
for i in pic_path:
i='{}/{}'.format(filename,i)
slide = ppt.slides.add_slide(ppt.slide_layouts[1])
slide.shapes.add_picture(i, Inches(0), Inches(0), Inches(10), Inches(7.5))
fname='{}/{}.pptx'.format(filename,filename)
ppt.save(fname)
print('生成的文件在 {} 文件夹下的 {}.ppt 中'.format(filename,filename))
示例2: to_ppt_slide
# 需要导入模块: from pptx import util [as 别名]
# 或者: from pptx.util import Inches [as 别名]
def to_ppt_slide(fig, file_path, append=False, padding=0.5):
from io import StringIO, BytesIO
import pptx
from pptx import Presentation
from pptx.util import Inches
# Create in-memory image stream and save figure to it
image_stream = BytesIO()
fig.savefig(image_stream)
if append:
try:
# Try opening the file if it already exists
prs = Presentation(file_path)
except pptx.exc.PackageNotFoundError:
prs = Presentation()
else:
prs = Presentation()
# Create a new slide with the blank template
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
# Center image without changing its aspect ratio
slide_width = prs.slide_width.inches - 2 * padding
slide_height = prs.slide_height.inches - 2 * padding
fig_width, fig_height = fig.get_size_inches()
if (fig_width / slide_width) > (fig_height / slide_height):
# Image fits slide horizontally and must be scaled down vertically
width = slide_width
height = width * fig_height / fig_width
top = padding + (slide_height - height) / 2
left = padding
else:
# Image fits slide vertically and must be scaled down horizontally
height = slide_height
width = height * fig_width / fig_height
left = padding + (slide_width - width) / 2
top = padding
# Convert from EMU to inches
left = Inches(left)
top = Inches(top)
height = Inches(height)
width = Inches(width)
pic = slide.shapes.add_picture(image_stream, left, top, height=height, width=width)
prs.save(file_path)