Skip to content

Instantly share code, notes, and snippets.

@iwatchme
Created June 6, 2022 06:02
Show Gist options
  • Save iwatchme/069044d438b56a11f3ab677f8eb8af70 to your computer and use it in GitHub Desktop.
Save iwatchme/069044d438b56a11f3ab677f8eb8af70 to your computer and use it in GitHub Desktop.
convert image to pdf
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment