PPT生成一页纸和二维码批量生成并插入到excel表格小工具
本帖最后由 myddb 于 2022-1-10 11:20 编辑1、将幻灯片合成一页纸;都是在根目录下生成文件夹,需要先把幻灯片转成png格式图片,直接把文件放到目录文件夹下运行即可;# 将幻灯片解压在ppt_joint目录下运行即可
import os
import time
from PIL import Image
def ppt():
while True:
path_ppt = (os.getcwd() + '\\ppt_joint\\')
if not os.path.exists(path_ppt):
os.mkdir(path_ppt)
cut_pictures = path_ppt
result_path_target = path_ppt + 'NEWS'
if not os.path.exists(result_path_target):
os.mkdir(result_path_target)
print('\n格式:1、图片要png或者PNG格式 2、文件名必须为幻灯片1、幻灯片2...或者幻灯片 (1)、幻灯片 (2)... 枚举方式 3、图片的清晰度与文件分辨率有关')
files_ppt = os.listdir(cut_pictures)
res=''
w = 0
for j in files_ppt:
if j != 'NEWS':
w = w+1
res = ((res + j).lstrip(',') +' ')
if len(files_ppt) <= 1:
print('\n检查:请将png文件放入excel内!')
else:
print('\n当前excel文件夹文件有:' + res)
res2 = ''
w1 = 0
for i in os.listdir(path_ppt + 'NEWS'):
w1 = w1 + 1
res2 = ((res2 +i).lstrip(',') +' ')
if len(os.listdir(path_ppt + 'NEWS'))<=0:
print('检查:当前的NEWS文件夹无一页纸文件!')
else:
print('当前NEWS文件夹文件有一页纸:' + res2)
print("\n0、刷新 1、合并幻灯片 2、清空ppt_joint下的png文件 3、退出\n")
input_1 = input('请输入需要的数字:')
if input_1 == "0":
print('刷新成功!')
if input_1 == "1":
ims = []
w= 0
files_ppt=os.listdir(cut_pictures)
if len(files_ppt) > 1:
num = input("请输入文件名:")#文件名
if num =='':
print('\n文件名不能为空!\n')
continue
for j in files_ppt:
if j == 'NEWS':
pass
if j.endswith(".PNG") or j.endswith(").png") or j.endswith(".png"):
w = w + 1
try:
imager=(Image.open(cut_pictures + '幻灯片' + w.__str__() + '.PNG'))
print('已合并:' + cut_pictures + '幻灯片' + w.__str__() + '.PNG')
except:
imager=Image.open(cut_pictures + '幻灯片 ({})'.format(w.__str__()) + '.png')
print('已合并:' + cut_pictures + '幻灯片 ({})'.format(w.__str__()) + '.png')
if imager.mode == 'P' or imager.mode == 'RGBA':
imager = imager.convert('RGB')
ims.append(imager)
#打开路径下的所有图片
width, height = ims[0].size# 获取拼接图片的宽和高
result = Image.new(ims[0].mode, (width, height * len(ims)))
for j, im in enumerate(ims):
result.paste(im, box=(0, j * height))
result.save(result_path_target + '/' + '%s.jpg' % num)
print ('\n一页纸图片已在NEWS文件夹生成!,1s后将自动返回主菜单。')
time.sleep(1)
else:
print("\n请将ppt转换为png图片格式放置在ppt_joint文件夹!,1s后将自动返回主菜单\n")
time.sleep(1)
elif input_1 == '2':
q = 0
if len(files_ppt) > 1:
for j in files_ppt:
if j == 'NEWS':
pass
if j.endswith(".PNG") or j.endswith(").png") or j.endswith(".png"):
q = q + 1
try:
os.remove(cut_pictures + '幻灯片' + q.__str__() + '.PNG')
print("已删除:" + cut_pictures + '幻灯片' + q.__str__() + '.PNG')
except:
os.remove(cut_pictures + '幻灯片 ({})'.format(q.__str__()) + '.png')
print("已删除:" + cut_pictures + '幻灯片 ({})'.format(q.__str__()) + '.png')
print("\n已清空所有png文件!1s后返回主菜单\n")
time.sleep(1)
continue
else:
print("\nppt_joint文件夹无png图片! 1s后返回主菜单\n")
time.sleep(1)
continue
elif input_1 == '3':
quit()
continue
try:
ppt()
except BaseException as e:
print('\n年年月月,热爱可抵岁月漫长。')
time.sleep(2)
pass
2、批量生成二维码并插入到excel表格中;
import os
import re
import shutil
import qrcode
from PIL import Image
from openpyxl import load_workbook
import xlsxwriter
class qrcodes():
def __init__(self):
self.file_img_path= os.getcwd() + '/二维码2/'
self.out_img_path = os.getcwd() + '/二维码/'
self.path = (os.getcwd() + '/excel/')
if not os.path.exists(self.path):
os.makedirs(self.path)
def baocun(self):
while True:
w = 0
li = []
for i in os.listdir(self.path):
if i != 'demo.xlsx':
w = w + 1
print(w.__str__() + '、' + i.__str__())
li.append(self.path + i)
if len(os.listdir(self.path)) <=1:
print("请将需要生成的文件放入excel文件夹中!")
self.a= input('\n请选择上方的xlsx文件号码或输入0刷新q退出:')
if self.a == '0':
print('刷新成功!')
continue
elif self.a == 'q':
quit()
elif self.a=='':
pass
else:
h = int(self.a)-1
self.filepath = (li).__str__()
with open(self.path + 'path.txt','w') as f:
f.write(self.filepath)
f.close()
break
def file_name(self):
with open(self.path + 'path.txt', "r") as r:
q = r.read()
return q
def file2(self):
self.filepath2 = (self.res_find(self.file_name(), self.p_data2)).__str__().replace('[\'', '').replace('\']','') + 'new.xlsx'
return self.filepath2
def biaotou(self):
# 创建excel文件
workbook = xlsxwriter.Workbook(self.path + 'demo.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
# 设置第一列的宽度
worksheet.set_column('A:A',33)
# 在A1单元格写入纯文本
worksheet.write('A1', '二维码',bold)
worksheet.write('A2', 'www.baidu.com')
workbook.close()
p_data = '2/(.*)'
p_data2 = '(.*).xlsx'
def res_find(self,data, pattern_data=p_data):
"""
查询
:param data:
:param pattern_data:
:return:
"""
pattern = re.compile(pattern_data)
re_res = pattern.findall(data)
return re_res
def file(self):
if os.path.exists(self.path + 'demo.xlsx'):
pass
else:
qrcodes().biaotou()
if len(os.listdir(self.path))<=1:
pass
def qrcode(self):
if not os.path.exists(self.file_img_path):
os.makedirs(self.file_img_path)
if not os.path.exists(self.out_img_path):
os.makedirs(self.out_img_path)
wb = load_workbook(self.file_name().replace('/','\\')) #打开工作簿
sheet = wb.active
rows = (len(list(sheet.rows)))
w=2
self.list_qrcode = []
for i in range(rows - 1):
i = i + w
img = qrcode.make(sheet.cell(i, 1).value)
self.imgname =('{}' + '{}' + '.jpg').format(i - 1 ,str(sheet.cell(i, 1).value).
replace('/','').replace('.','').replace('*','').replace(':',''))
self.img_save = (self.file_img_path + self.imgname)
print('已生成图片:' + self.img_save)
img.save(self.img_save)
self.list_qrcode.append(self.img_save)
width = 180;height = 180;type='png'
self.add_img_list = []
for i in self.list_qrcode:
img = Image.open(i)
out = img.resize((width, height), Image.ANTIALIAS)
q = (qrcodes().res_find(i).__str__().replace('[\'','').replace('\']',''))
self.add_img_list.append(q)
out.save(self.out_img_path +q, type)
return self.add_img_list
def add_img(self):
from openpyxl.drawing.image import Image
self.num=0
self.w = 1
wb = load_workbook(self.file_name())
for i in self.qrcode():
self.num = self.w + self.num
img = Image((self.out_img_path +i)) #需添加的图片所在路径
# img = Image(i)
ws = wb['Sheet1']
ws.add_image(img,'B' + ((self.num + 1).__str__()))
print("已插入图片:" + self.out_img_path +i)
ws.column_dimensions['B'].width = 23
ws.row_dimensions[(self.num + 1)].height = 136
wb.save(self.file2())
def del_l(self):
shutil.rmtree(self.out_img_path)
shutil.rmtree(self.file_img_path)
os.remove(self.path + 'path.txt')
def ty(self):
try:
print(
'来看看规矩:\n1、表格内容可参考excel下的demo格式!\n2、sheet表名要保证为Sheet1!\n3、只支持xlsx格式其他格式不行!\n4、选择你的文件号码填一个数字就行!\n')
while True:
qrcodes().biaotou()
qrcodes().file()
qrcodes().baocun()
qrcodes().add_img()
qrcodes().del_l()
print('\n已生成文件!\n')
if input('退出请输入q,否则返回首页!\n') == 'q':
break
except BaseException:
pass
finally:
try:
qrcodes().del_l()
except BaseException:
pass
qrcodes().ty()
如何更改 PowerPoint 幻灯片导出为图片的输出分辨率;
https://www.jianshu.com/p/6a06e6d21838
无聊写的小工具!:victory: 这个有用!{:1_921:}{:1_921:}{:1_919:} 这个强大 厉害了,比较实用 感谢大佬分享,这个绝对是实用的干货 实用利器,感谢分享 不懂这个功能的使用场景是什么 谢谢分享{:1_918:} 这个有很大用处 一直想要找,感谢分享!!!!!!
页:
[1]
2