from ast import main from cmath import e import os import img2pdf def convertImgsToPdf(dir_name, result_pdf): imgs =[] for fname in os.listdir(dir_name): if fname.lower().endswith(".png") and not fname.lower().startswith("._"): imgs.append(os.path.join(dir_name, fname)) imgs = sorted(imgs) with open(result_pdf,"wb") as f: try: f.write(img2pdf.convert(imgs)) except Exception as e: print(e) def findNotDirectory(rootDir): for root, dirs, files in os.walk(rootDir): if dirs: for dir in dirs: findNotDirectory(os.path.join(root, dir)) else: print(os.path.join(root)) convertImgsToPdf(root, root+".pdf") def main(): root = "/Users/iwatchme/Desktop/2/20211009 很帅的合辑" findNotDirectory(root) main()