2023年7月25日发(作者:)
Reportlab初体验reportlab介绍reportlab是Python的⼀个标准库,可以画图、画表格、编辑⽂字,最后可以输出PDF格式。它的逻辑和编辑⼀个word⽂档或者PPT很像。有两种⽅法:1)建⽴⼀个空⽩⽂档,然后在上⾯写⽂字、画图等;2)建⽴⼀个空⽩list,以填充表格的形式插⼊各种⽂本框、图⽚等,最后⽣成PDF⽂档。我因为产⽣⼀份给⽤户看的报告,⾥⾯需要插⼊图⽚、表格等,所以采⽤的是第⼆种⽅法。建⽴⽂本from e import pdfmetricsfrom s import TTFontfrom import getSampleStyleSheet,ParagraphStylefrom us import SimpleDocTemplate, Paragraph, Spacer,Image,Table,TableStyle,Frame,ListFlowable, ListItemfrom import TA_JUSTIFYfrom import colorsfrom import CMYKColorstory=[]#建⽴空⽩的erFont(TTFont('msyh', '/Users/cello/Library/Fonts/'))### 设置中⽂字体名称为msyhstyles = getSampleStyleSheet()#获得reportlab预先设定的⽂本模板(ParagraphStyle(name='txt', leftIndent=-50,rightIndent=-50,alignment=TA_JUSTIFY,fontName="msyh",fontSize=8,textColor='#003153',bulletFontSize=12, bulletIndent=-50,bulletAnchor ='start',bulletFontName = 'Symbol' ))#添加新的段落⽂本风格模板,并且起名字为‘txt'
# 更多可以配置的模板参数见reportlab-userguide Chapter 6 Paragraphs
text = '''
第⼀部分:建⽴⽂本说明
", styles["txt"]), bulletColor=CMYKColor(0, 0, 0, 0)), # 空⼀⾏,将bullet的颜⾊设置为和背景⼀样的 ListItem(Paragraph('⼩蓝XXXXXXXXXXXX', styles["txt"]), leftIndent=10, value='diamondwx',bulletFontSize=6, bulletColor=CMYKColor(0.81, 0.45, 0.53, 0.23)),], start='sparkle', leftIndent=60)(my_list )画图与图⽚画图虽然reportlab也可以画图,不过我嫌⿇烦,就⽤的另外⼀个Python包:pyecharts,⽤pyecharts⽣成图⽚后在读取图⽚from pyecharts import Stylefrom pyecharts import Gaugestyle_gauge = Style( title_color="#003153", title_pos="center", width=1100, height=600, title_text_size=26, title_top='bottom') #设置图的风格,包括title的颜⾊、位置、字体及⼤⼩,图⽚的宽与⾼# 更多可以配置的参数见:/#/zh-cn/charts_configuregauge = Gauge('仪表图',**style__style) #初始化('', "偏好程度", 44, scale_range=[0, 100],is_legend_show=False)#画图(path+'.png')#输出png图⽚到path添加图⽚img = Image(path+'.png') # 读取特定路径的图⽚ight =5*cm #设置读取后图⽚的⾼dth = 8*cm #设置读取后图⽚的宽(img)# 将图⽚存储到list中画表格# 添加表格样式table_style,9⾏3列,第⼀⾏为标题table_style = [ ('FONTNAME', (0, 0), (-1, -1), 'msyh'), # 字体 ('FONTSIZE', (0, 0), (2, 0), 8), # 第⼀⾏的字体⼤⼩ ('FONTSIZE', (0, 1), (-1, -1), 8), # 第⼆⾏到最后⼀⾏的字体⼤⼩ ('ALIGN', (0, 0), (2, 0), 'CENTER'), # 第⼀⾏左右中间对齐 ('ALIGN', (0, 1), (2, 8), 'LEFT'), # 第⼆⾏到最后⼀⾏左右左对齐 ('VALIGN', (0, 0), (2, 8), 'MIDDLE'), # 所有表格上下居中对齐 ('SPAN', (0, 1), (0, 2)), # 合并第⼀列⼆三⾏ ('SPAN', (0, 3), (0, 4)), # 合并第⼀列三四⾏ ('SPAN', (0, 5), (0, 6)), # 合并第⼀列五六⾏ ('SPAN', (0, 7), (0, 8)), # 合并第⼀列五六⾏ ('BACKGROUND', (0, 0), (2, 0), lategray), # 设置第⼀⾏背景颜⾊ ('TEXTCOLOR', (0, 0), (-1, -1), ategray), # 设置表格内⽂字颜⾊ ('GRID', (0, 0), (-1, -1), 0.1, ray), # 设置表格框线为灰⾊,线宽为0.1]# 表格数据:⽤法详见中chapter 7 Tabletable_data = [['年', '⽉', '⽇'], ['2017', '3', '12'], ['2017', '4', '13'], ['2017', '5', '14'], ['2017', '6', '15'], ['2018', '7', '16'], ['2018', '8', '17'], ['2018', '9', '18'], ['2018', '10', '19'], ]# ⽣成表格table_table = Table(table_data, colWidths=[42, 38, 147], style=component_style)#将数据套到已经设定好的表格样式中,并且设置列宽(table_table)#将表格添加到list:story 中添加空格(Spacer(240, 10))#添加空⽩,长度240,宽10⽣成⽂档doc = SimpleDocTemplate(path +'test'+'.pdf',pagesize=[10,20],topMargin = 15,bottomMargin = 15)(story)
发布者:admin,转转请注明出处:http://www.yc00.com/news/1690216909a316311.html
评论列表(0条)