gpt4 book ai didi

python - TypeError at/app/profile/, 'list' 对象不可调用 handle_pageBegin args=()

转载 作者:行者123 更新时间:2023-12-04 15:19:10 24 4
gpt4 key购买 nike

我想要一个按钮来创建一个 PDF 文件(使用 Python 2.7,Django 1.8),但我不知道如何将它表达给机器。

我开始阅读一些文章,试图向其他人学习如何做到这一点。

找到 this handy link ,这将我重定向到以下链接:

  • ReportLab on BitBucket

  • 因此,为了打印为 PDF:

    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 方法一:使用ReportLab /////////////////////////////////////////////////
    pip install reportlab

    ReportLab 依赖 Pillow,它是一个第三方 Python 图像库。
    如果你运行:
    pip install Pillow

    你会看到这样的东西:
    Requirement already satisfied: Pillow in c:\pythonprojects\virtualenvs\apis\lib\site-packages
    Requirement already satisfied: olefile in c:\pythonprojects\virtualenvs\apis\lib\site-packages (from Pillow)

    在这个级别似乎不需要其他任何东西,所以让我们首先确保我们在 View 中拥有我们需要的一切。

    ------------------------------------------ 版本 0.2 (代码错误)------------------------------------------

    1. 代码
    # views.py
    from django.http import response
    from django.core.files.storage import FileSystemStorage
    from django.shortcuts import render, HttpResponse
    from django.contrib.auth.decorators import login_required
    from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
    from reportlab.lib.styles import getSampleStyleSheet
    from reportlab.lib.units import inch
    import requests
    import praw


    def profile(request):
    #PDF
    doc = SimpleDocTemplate("test.pdf")
    styles = getSampleStyleSheet()
    Story = [Spacer(1, 2 * inch)]
    style = styles["Normal"]

    #Webpage
    parsedData = []
    userData = {}
    if request.method == 'POST':
    #Data
    sub = request.POST.get('subreddit')
    reddit = praw.Reddit(client_id='client_id',
    client_secret='client_secret',
    user_agent='user_agent')
    # assuming you have a Reddit instance bound to variable `reddit`
    subreddit = reddit.subreddit(sub)

    print('Master, this was the data retrieved that is going to be displayed:')
    print(subreddit.display_name) # Output: redditdev
    print(subreddit.title) # Output: reddit Development
    print(subreddit.description) # Output: A subreddit for discussion of ...
    print('Tell me if you want more and I can give that to you, no problem-------------')

    #PDF
    print('Now, Im gonna create a PDF file with the same data...')
    subreddit_name = subreddit.display_name
    subreddit_title = subreddit.title
    subreddit_description = subreddit.description

    p = Paragraph(subreddit_name, subreddit_title, subreddit_description, style)
    Story.append(p)
    Story.append(Spacer(1, 0.2 * inch))
    doc.build(Story)
    print('The file was created and can be accessed in the root of the Django project. The name is test.pdf')
    print('Tell me to stop if you dont like what Im doing-------------')

    #Webpage
    print('OK. Time to give that to the user as well...')
    userData['subreddit_name'] = subreddit.display_name
    userData['title'] = subreddit.title
    userData['description'] = subreddit.description
    parsedData.append(userData)
    return render(request, 'app/profile.html', {'data': parsedData})

    从那一刻起 #PDF下的代码被插入, View 永远不会相同。

    2. 错误

    回溯为图像:
    enter image description here

    回溯为文本:
    Environment:


    Request Method: POST
    Request URL: http://127.0.0.1:8000/app/profile/

    Django Version: 1.8
    Python Version: 2.7.12
    Installed Applications:
    ('frontend',
    'app',
    'blog',
    'suit',
    'suit_redactor',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles')
    Installed Middleware:
    ('django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware')


    Traceback:
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\django\core\handlers\base.py" in get_response
    132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
    22. return view_func(request, *args, **kwargs)
    File "C:\PythonProjects\reddit_development\demonstration\app\views.py" in profile
    69. doc.build(Story)
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\platypus\doctemplate.py" in build
    1213. BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker)
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\platypus\doctemplate.py" in build
    969. self.handle_flowable(flowables)
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\platypus\doctemplate.py" in handle_flowable
    834. if frame.add(f, canv, trySplit=self.allowSplitting):
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\platypus\frames.py" in _add
    167. w, h = flowable.wrap(aW, h)
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\platypus\paragraph.py" in wrap
    1191. leftIndent = style.leftIndent

    Exception Type: AttributeError at /app/profile/
    Exception Value: 'unicode' object has no attribute 'leftIndent'

    3. 我认为问题出在哪里?:

    代码运行良好,直到这部分:
    p = Paragraph(subreddit_name, subreddit_title, subreddit_description,  style)
    Story.append(p)
    Story.append(Spacer(1, 0.2 * inch))
    doc.build(Story)

    ------------------------------------------ 版本 0.3 ----------------------------------------------

    据此 Christoph Bluoss在他的消息中并致 the documentation ,
    p = Paragraph("Some string", style)

    知道了这一点,

    1. 代码

    一种。创建了 3 个类
    Story1 = [Spacer(1, 2 * inch)]

    Story2 = [Spacer(1, 2 * inch)]

    Story3 = [Spacer(1, 2 * inch)]

    湾创建了 3 个新变量
    p1 = Paragraph(subreddit_name,  style)

    p2 = Paragraph(subreddit_title, style)

    p3 = Paragraph(subreddit_description, style)

    C。将所有变量附加​​到不同的类中
    Story1.append(p1)
    Story1.append(Spacer(1, 0.2 * inch))

    Story2.append(p2)
    Story2.append(Spacer(1, 0.2 * inch))

    Story3.append(p3)
    Story3.append(Spacer(1, 0.2 * inch))

    d.用这些变量做了一个 doc.buil 。
    doc.build(Story1, Story2, Story3)

    2. 错误

    回溯为图像:
    enter image description here

    回溯为文本:
    Environment:


    Request Method: POST
    Request URL: http://127.0.0.1:8000/app/profile/

    Django Version: 1.8
    Python Version: 2.7.12
    Installed Applications:
    ('frontend',
    'app',
    'blog',
    'suit',
    'suit_redactor',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles')
    Installed Middleware:
    ('django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware')


    Traceback:
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\django\core\handlers\base.py" in get_response
    132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
    22. return view_func(request, *args, **kwargs)
    File "C:\PythonProjects\reddit_development\demonstration\app\views.py" in profile
    77. doc.build(Story1, Story2, Story3)
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\platypus\doctemplate.py" in build
    1213. BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker)
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\platypus\doctemplate.py" in build
    966. self.clean_hanging()
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\platypus\doctemplate.py" in clean_hanging
    554. self.handle_flowable(self._hanging)
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\platypus\doctemplate.py" in handle_flowable
    828. f.apply(self)
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\platypus\doctemplate.py" in apply
    153. annotateException("\nhandle_%s args=%s"%(action,ascii(args)))
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\lib\utils.py" in annotateException
    1390. rl_reraise(t,v,b)
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\platypus\doctemplate.py" in apply
    146. getattr(doc,arn)(*args)
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\platypus\doctemplate.py" in handle_pageBegin
    1189. self._handle_pageBegin()
    File "C:\PythonProjects\virtualenvs\apis\lib\site-packages\reportlab\platypus\doctemplate.py" in handle_pageBegin
    579. self.pageTemplate.onPage(self.canv,self)

    Exception Type: TypeError at /app/profile/
    Exception Value: 'list' object is not callable
    handle_pageBegin args=()

    3. 我认为问题出在哪里:

    代码运行良好,直到这部分:
     p1 = Paragraph(subreddit_name,  style)
    p2 = Paragraph(subreddit_title, style)
    p3 = Paragraph(subreddit_description, style)
    Story1.append(p1)
    Story1.append(Spacer(1, 0.2 * inch))
    Story2.append(p2)
    Story2.append(Spacer(1, 0.2 * inch))
    Story3.append(p3)
    Story3.append(Spacer(1, 0.2 * inch))
    doc.build(Story1, Story2, Story3)

    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 方法二:使用WeasyPrint /////////////////////////////////////////////////

    如果我们不能用过去的方法修复,这似乎是一笔不错的交易。

    最佳答案

    回答您更新的问题 (v 0.3)
    根据文档 http://www.reportlab.com/docs/reportlab-userguide.pdf (第 68 页):

    BaseDocTemplate.build(self, flowables, filename=None, canvasmaker=canvas.Canvas)

    Assuming that the document instance is correctly set up the build method takes the story in the shape of the list of flowables (the flowables argument) and loops through the list forcing the flowables one at a time through the formatting machinery



    而另一方面,您只是使用几个列表调用 build() 。实际上,我什至不明白您为什么要使用 3 个单独的列表,而不仅仅是 单单像这样首先。
    story = [Spacer(1, 2 * inch)]
    story.append(Paragraph(subreddit_name, style))
    story.append(Spacer(1, 2 * inch))
    story.append(Paragraph(subreddit_title, style))
    story.append(Spacer(1, 2 * inch))
    story.append(Paragraph(subreddit_description, style))
    doc.build(story)

    这是工作正常。 (虽然用python3测试过)

    关于python - TypeError at/app/profile/, 'list' 对象不可调用 handle_pageBegin args=(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42717591/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com