使用python全屏截图并将图片自动添加到ppt中

首先安装需要的包

pip install python-pptx #用于操作ppt
pip install keyboard #用于监听热键
pip install Pillow #用于截图

代码如下

import keyboard  #监听键盘
from PIL import ImageGrab #截图
from pptx import Presentation #操作ppt
from pptx.util import Inches, Pt
import uuid
import time
import os

ppt_name = str(uuid.uuid1())+r'.pptx'
ppt = Presentation()#实例化Presentation类,构建一个名为ppt的对象
def screenshot():
	'''截图'''
	path='screenshots'
	if not os.path.exists(path):
		os.mkdir(path)
	im = ImageGrab.grab() #截图全屏
	img_name='screenshots/'+str(int(round(time.time() * 1000)))+'.png'
	im.save(img_name)
	blank_slide_layout = ppt.slide_layouts[6] #空白页
	slide = ppt.slides.add_slide(blank_slide_layout)
	left = Inches(0.0) 
	top = Inches(1.0)
	width = Inches(10.0)
	height = Inches(6.0)
	slide.shapes.add_picture(img_name, left, top, width, height)
	ppt.save(ppt_name)

if __name__ == '__main__':
	keyboard.add_hotkey('ctrl+alt', screenshot)#按ctrl+alt截图
	keyboard.wait('alt+x')#wait里也可以设置按键,说明当按到该键时结束
	#ppt.save(ppt_name)
posted @ 2021-09-16 16:30  风轻云断  阅读(699)  评论(0编辑  收藏  举报