You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ReportLab Canvas的Paragraph时出现AttributeError:无decode属性问题

解决ReportLab Paragraph的AttributeError(无decode属性)问题

这个错误的核心原因是实例化Paragraph时缺少必填的样式参数。在Python 3环境下,ReportLab的Paragraph类要求必须传入文本内容和对应的样式对象,仅传文本会导致内部逻辑错误,触发decode相关的属性报错(Python 3中字符串默认是Unicode,不存在decode方法)。

修正步骤:

  • 先导入ParagraphStyle类,创建一个基础样式
  • 实例化Paragraph时同时传入文本和样式对象
  • 确保wrapOn和drawOn的参数与Canvas对象匹配

修正后的代码示例:

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

# 创建基础样式
style = ParagraphStyle(name='Normal')

# 实例化Paragraph时传入文本和样式
desc = Paragraph("Hi everyone", style)

# 假设c是已初始化的Canvas对象,width、height、x、y为合理数值
desc.wrapOn(c, 400, 200)  # 调整为你需要的宽度和高度
desc.drawOn(c, 50, 50)    # 调整为你需要的坐标

如果需要多行文本,直接在Paragraph的文本参数里用换行标签<br/>即可,比如:

desc = Paragraph("Hi everyone<br/>This is a multi-line text", style)

内容的提问来源于stack exchange,提问作者Rusca8

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 23:05:53