异常值:参数文本的类型无效

时间:2016-07-16 02:10:34

标签: python pdf reportlab

我正在尝试使用reportlab获取一个段落,但我无法让它工作。

此代码可以正常工作:

p.setFont('Helvetica',8)
labo = str('CANCIÓN').decode('utf-8')
p.setFillColor(HexColor('#ff8100'))
p.drawString(350,736, labo)

但是这段代码没有:

styles = getSampleStyleSheet()
labo = Paragraph("Generating Reports with Python", styles["Heading1"])
p.drawCentredString(400,600, labo)

它返回:

Exception Value:    invalid type for argument text

我做错了什么?

我想我已经导入了所有必要的模块。

#!/usr/bin/python
# -*- encoding: utf-8 -*-

from reportlab.pdfgen import canvas
from django.http import HttpResponse
from reportlab.lib.pagesizes import letter
from reportlab.lib.colors import HexColor
from reportlab.lib.utils import ImageReader

from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph

import os
from io import BytesIO
import PIL.Image

from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

1 个答案:

答案 0 :(得分:0)

您收到此错误的原因是您正在混淆语法。 interface Function<T,R> { R apply(T t) } interface Consumer<T> { void accept(T t) } interface BiFunction<T,U,R> { R apply(T t, U u) } 中使用了Paragraph,而Platypus是基本的画布操作。

drawCentredString的语法为drawCentredString,希望您将文字作为canvas.drawCentredString(x, y, text)提供,而不是string对象。

Paragraph的语法不同,它应该如下所示:

Paragraph

因此,在我们制作段落后,我们会告诉它使用p = Paragraph("Generating Reports with Python", styles["Heading1"]) p.wrapOn(canvas, 200, 400) p.drawOn(canvas, 400, 600) 可以使用多少空间。之后我们使用wrapOn将其绘制到画布上。

但是,按照上面提到的方式进行操作仅使用drawOn(因此Platypus)的一小部分功效。它可以用来处理文档的流程而不只是一个Paragraph,因此您可能需要查看Reportlab Userguide的第5章,它以明确的方式解释了它的用法和好处。