找不到load_resource函数作为FPDF的类方法 [英] load_resource function not found as a class method of FPDF

查看:122
本文介绍了找不到load_resource函数作为FPDF的类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找以下问题的答案:将Base64图像插入pdf使用pyfpdf

I am looking at the answer to the following question: Insert Base64 image to pdf using pyfpdf

此处建议的答案是覆盖现有的load_resource方法.

The answer suggested here was to override the existing load_resource method.

我所做的是

class EnhancedPdf(FPDF):

    def load_resource(self, reason, filename):
        if reason == "image":
            if filename.startswith("data"):
                f = filename.split("base64,")[1]
                f = base64.b64decode(f)
                f = BytesIO(f)
                return f
            else:
                return super().load_resource(reason, filename)

但是,Pycharm使用消息类FPDF"的未解决的属性引用" load_resource"突出显示了超级调用

However, Pycharm highlights the super call with the message "Unresolved attribute reference "load_resource" for class "FPDF"

在命令行中,我运行了命令

In my command line, I ran the commands

from fpdf import FPDF
   dir(FPDF)

检查此列表,我发现load_resource函数确实不是列出的方法.因此,我的问题是为什么load_resource函数不可见?

Inspecting this list, I see load_resource function is indeed not a listed method. Hence my question is why is the load_resource function not visible?

推荐答案

很可能您正在使用Python 3.x,其中x> = 5.

Most probably you are using Python 3.x where x >= 5 .

在pypi上说该模块仅对python 3.y实验支持,其中y< = 4.

On the pypi it says that the module has only experimental support for python 3.y where y <= 4 .

尝试使用python 2.7,它可能会起作用.

Try it with python 2.7 and it might work.

PS:最好尝试 https://pypi.org/project/fpdf2/,更新后的版本.有关错误或问题,请参见 https://github.com/alexanderankin/pyfpdf .

PS: Better try https://pypi.org/project/fpdf2/, the updated version. For bugs or issues see https://github.com/alexanderankin/pyfpdf .

如果您确实要使用旧版本,则可以从原始存储库中安装所需的任何版本,例如

If you really want to use the old version, you can install whatever version you want from the original repo like this

pip install git+https://github.com/reingart/pyfpdf@<branchname of tag or commit> 

这篇关于找不到load_resource函数作为FPDF的类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆