2016-07-16 51 views
0

我試圖獲得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 

回答

0

你得到這個錯誤的原因是你在混合語法。 Paragraph用於Platypus,而drawCentredString是基本的畫布操作。

的語法爲drawCentredStringcanvas.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)的權力的一小部分。它可以用來處理完整的文檔流,而不是隻有一個Paragraph,所以你可能想看看Reportlab Userguide的第5章,它清楚地解釋了它的用法和優點。

相關問題