使用Report Lab Python库时出现属性错误

时间:2019-09-26 13:26:00

标签: python

使用报表实验室将文本文件转换为PDF时,出现以下错误:

  

canvas.drawString(15,i,linea.strip())   AttributeError:模块'reportlab.pdfgen.canvas'没有属性'drawString'

我不确定自己在做什么错。我尝试重新安装报告实验室,但这不能解决问题。

from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader
from PIL import Image
    while numeroLinea < len(lineas):
        if numeroLinea - len(lineas) < 60:  # I'm gonna write every 60 lines because I need it like that
            i = 750
            for linea in lineas[numeroLinea:numeroLinea + 60]:
                canvas.drawString(15, i, linea.strip())
                numeroLinea += 1
                i -= 12
            canvas.showPage()
        else:
            i = 750
            for linea in lineas[numeroLinea:]:
                canvas.drawString(15, i, linea.strip())
                numeroLinea += 1
                i -= 12
            canvas.showPage()

1 个答案:

答案 0 :(得分:0)

在您的情况下,canvas是从reportlab.pdfgen导入的模块。该模块包含类Canvas。您应该创建此类的对象以访问此类的方法,例如drawString()showPage()等。例如:

from reportlab.pdfgen import canvas

PATH_TO_PDF = '/media/data/sample.pdf' # path to your pdf file
can = canvas.Canvas(PATH_TO_PDF)
can.drawString(...)
can.showPage(...)