#python两侧加图片源文件路径可选择
##pptUpdateChoosePath.py
优化项
1.源文件可弹出窗口选择文件
2.图片位置保持固定
3.输出文件设置为源文件所在目录加时间戳
#python3.10需要导入...abc,不然会报错
import collections.abc
#导入pptx模块
from pptx import Presentation
from pptx.util import Inches
#弹窗选择所需要修改源文件
import tkinter as tk
from tkinter import filedialog
#需要获取当前文件的绝对路径导入模块
import os
import re
# 导入time模块
import time
'''---------- tkinter -----------'''
# tkinter实例化
root = tk.Tk()
root.withdraw()
# 获取文件夹路径
f_path = filedialog.askopenfilename()
'''---------- pptx -----------'''
#打开文件
prs = Presentation(f_path)#"D:/Test/test/0809热线.pptx"
#获取当前页面宽度
wid =prs.slide_width.inches;
hei = prs.slide_height.inches;
#第一步 将ppt幻灯片大小 将页面宽度拉宽
prs.slide_width = Inches(wid*2.1225);
#第二步 将ppt母版外元素移到中间
for slide in prs.slides:
# print(slide)
for shape in slide.shapes:
# print(shape.left)
shape.left = shape.left+Inches(wid*0.56125);
#尝试添加图片add_picture(path, x, y, cx, cy)#(url,left,top,width,height)
#url = filedialog.askopenfilename();
url = "C:/Users/HUAWEI/Desktop/节日主题/123.png";
left = top = Inches(0);
width = Inches(wid*0.56125);
height = Inches(hei);
picture = slide.shapes.add_picture(url,left,top,width,height);
picture1 = slide.shapes.add_picture(url, left+Inches(wid+wid*0.56125), top, width, height);
#第三步 尝试将母版元素移到中间
for layouts in prs.slide_masters:
for slide in layouts.slide_layouts:
for sh in slide.shapes:
# print(sh.left)
sh.left = sh.left +Inches(wid*0.56125);
'''---------- os -----------'''
#获取当前文件所在目录
path = os.path.dirname(os.path.abspath(__file__));
#获取源文件名
newName = re.search(r'[^\\/:*?"<>|\r\n]+$',f_path)
f_pathName = newName.group();
#拼接当前文件所在目录和源文件名加时间戳
# 优化格式化化版本时间戳
#print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
fTime = time.strftime('_%Y%m%d%H%M%S.',time.localtime(time.time()));
#生成当前文件所在目录和源文件名加时间戳
#targetPath = path+'\\'+f_pathName.replace('.',fTime);
#生成源文件所在目录和源文件名加时间戳
targetPath = f_path.replace('.',fTime);
print(targetPath);
#另存为新的ppt文件
prs.save(targetPath);#C:/Users/HUAWEI/Desktop/sym/20211.pptx
print("成功!");