Python Image Library 無法轉換為 pdf



我有一个可以用枕头将BMP转换为PDF的函数,此脚本我将其包含在非编译版本和编译版本(.exe(中。在第一个中,它可以正常工作,但是在第二个枕头中会抛出异常('pdf'(。在.save((中具体失败带有扩展名的路径和文件名是正确的。

from PIL import Image
def bmp2pdf(self, file):
    ''' Convert a bmp file to PDF file, and delete old bmp file '''
    img = Image.open(file)
    output = file.replace('.bmp', '.pdf')
    try:
        img.save(output, "PDF", resolution=100.0)
        remove(file)
    except Exception as err:
        print(err)

在编译版本中,输出为:

'PDF'

thx。

遵循此代码。它有效。3行代码。

from PIL import Image def bmp2pdf(self,path): img = Image.open(path) img.save('image.pdf','pdf')

我在其中有一个名为image.pdf的文件。

我必须在设置中添加以生成.exe我应该导入pil而不是pil.image,因此整个模块已加载并且PDF功能可用

我正在使用cx_freeze:

'Packages': [
    'PyQt5.uic',
    "...",
    'PIL',
]

最新更新