名称错误: 未定义全局名称'styles'



这部分代码Paragraph('Road',style["Normal"], bulletText=None)给了我这个NameError: global name 'styles' is not defined.我导入的模块是:

from reportlab.lib.styles import ParagraphStyle

首先确保你已经定义了这样的样式:

from reportlab.lib.styles import getSampleStyleSheet
styles = getSampleStyleSheet()

你也可以添加其他样式,比如"Justify"
你可以这样做(例如使用"Justify"):

from reportlab.lib.enums import TA_JUSTIFY
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
text = "Hello World !"
story.append(Paragraph(text, style["Justify"]))

然后这样使用:

# create pdf
pdf = SimpleDocTemplate("your_doc.pdf")
# write in it
story = []
story.append(Paragraph(text, style["Justify"]))    
# save it
pdf.build(story)
# return
return (frame)
这就是你的pdf样式了!

最新更新