[Python] Converção de imgens em PDF com Python

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    Estou tentando converter todas as imagens de um diretório, em um único PDF. Porém estou com dificuldades.

    Quando executo meu sistema ele da o erro abaixo, ele tenta ler o arquivo .py também.

    from PIL import Image
    import os
    filepath = os.getcwd()

    for p in os.listdir(filepath):

    filename = filepath+'\\'+p

    im = Image.open(filename)

    if im.mode == "RGB":
    im = im.convert("RGB")
    new_file = "novo.pdf"
    new_filename = os.getcwd()
    file = new_filename+"\\"+new_file
    if not os.path.exists(new_filename):
    im.save(file, "PDF")


    Traceback (most recent call last):
    File "C:/crl/pdf/teste.py", line 9,
    in <module> im = Image.open(filename)
    File "C:\Users\willian.angelo\anaconda3\lib\site-packages\PIL\Image.py", line 2687,
    in open % (filename if filename else fp)) OSError: cannot identify image file 'C:\\crl\\pdf\\teste.py'

    Continue reading...

Compartilhe esta Página