本文整理匯總了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)