load_resource function not found as a class method of FPDF

妖精的绣舞 提交于 2020-01-15 09:16:06

问题


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

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

What I did instead was

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)

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)

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?


回答1:


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

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

Try it with python 2.7 and it might work.

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> 


来源:https://stackoverflow.com/questions/59609882/load-resource-function-not-found-as-a-class-method-of-fpdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!