1.降低像素
代码逻辑是:先选出文件夹里的JPG文件,降低像素(大概300k左右)
from PIL import Image
import glob, os
#降低像素,方便后面合并,
for files in glob.glob(r'D:\发票\fp\*.JPG'):
filepath,filename = os.path.split(files)
filterame,exts = os.path.splitext(filename)
opfile = r"D:\发票\cs" #输出路径
im = Image.open(files)
w,h = im.size
im_ss = im.resize((int(w*0.5), int(h*0.5)))
im_ss.save(opfile+"\\"+ filterame+'.JPG') #注意路径要双斜杠或者反斜杠
2.合并成PDF
from fpdf import FPDF
from PIL import Image
import os
def makePdf(pdfFileName,listPages,dizhi):
cover = Image.open(dizhi+ listPages[0])
width, height = cover.size
pdf = FPDF(unit = "pt", format = [width, height])
for page in listPages:
pdf.add_page()
pdf.image(dizhi+page,0, 0)
pdf.output(pdfFileName, "F")
合并用了29秒。pdf已经生成好了,只需要打印pdf文件就可以打印出几百张发票了,任务完成。
前面也可以做个操作方便以后存档,把文件里的JPG全部移到新文件里。直接上代码吧:
import os
import glob
import shutil
path = r'D:\发票\fp' #原始文件路径
path_new = r'D:\发票\fl' #目标文件路径
list_name = os.listdir(path)
print(list_name)
print(len(list_name))
for f in os.listdir(path):
filename = os.path.join(path,f)
if f.split(".")[-1] == "JPG":
print(f)
shutil.move(filename,path_new)
print("done")