python中的Reportlab模塊詳解最新推薦
python中的Reportlab模塊
reportlab模塊是用python語言生成pdf文件的模塊
安裝:pip install reportlab
模塊默認(rèn)不支持中文,如果使用中文需要注冊(cè)
1.注冊(cè)中文字體
下載自己需要的.ttf字體,例如STSONG.ttf
fromreportlab.pdfbaseimportpdfmetrics
fromreportlab.pdfbase.ttfontsimportTTFont
pdfmetrics.registerFont(TTFont('song', STSONG.ttf))
2.生成文字
from reportlab.lib.styles import getSampleStyleSheet from reportlab.platypus import Paragraph,SimpleDocTemplate from reportlab.lib import colors Style=getSampleStyleSheet() bt = Style['Normal'] #字體的樣式 # bt.fontName='song' #使用的字體 bt.fontSize=14 #字號(hào) bt.wordWrap = 'CJK' #該屬性支持自動(dòng)換行,'CJK'是中文模式換行,用于英文中會(huì)截?cái)鄦卧~造成閱讀困難,可改為'Normal' bt.firstLineIndent = 32 #該屬性支持第一行開頭空格 bt.leading = 20 #該屬性是設(shè)置行距 ct=Style['Normal'] # ct.fontName='song' ct.fontSize=12 ct.alignment=1 #居中 ct.textColor = colors.red t = Paragraph('hello',bt) pdf=SimpleDocTemplate('ppff.pdf') pdf.multiBuild([t])
一份pdf文件可以定義多種字體樣式,如bt和ct。字體有多種屬性,這里只列舉一些常用的屬性,
其中,
wordWrap自動(dòng)換行屬性的參數(shù)'CJK'是按照中文方式換行(可以在字符之間換行),英文方式為'Normal'(在空格出換行)
alignment:0 左對(duì)齊
1 居中
2 右對(duì)齊
3.表格
from reportlab.platypus import Table
t = Table(data)
from reportlab.platypus import Paragraph, SimpleDocTemplate, Table,TableStyle from reportlab.lib.units import inch from reportlab.lib import colors def table_model(data): width = 7.2 # 總寬度 colWidths = (width / len(data[0])) * inch # 每列的寬度 dis_list = [] for x in data: # dis_list.append(map(lambda i: Paragraph('%s' % i, cn), x)) dis_list.append(x) style = [ # ('FONTNAME', (0, 0), (-1, -1), 'song'), # 字體 ('FONTSIZE', (0, 0), (-1, 0), 15), # 字體大小 ('BACKGROUND', (0, 0), (-1, 0), HexColor('#d5dae6')), # 設(shè)置第一行背景顏色 ('BACKGROUND', (0, 1), (-1, 1), HexColor('#d5dae6')), # 設(shè)置第二行背景顏色 # 合并 ('SPAN',(第一個(gè)方格的左上角坐標(biāo)),(第二個(gè)方格的左上角坐標(biāo))),合并后的值為靠上一行的值,按照長(zhǎng)方形合并 ('SPAN',(0,0),(0,1)), ('SPAN',(1,0),(2,0)), ('SPAN',(3,0),(4,0)), ('SPAN',(5,0),(7,0)), ('ALIGN', (0, 0), (-1, -1), 'CENTER'), # 對(duì)齊 ('VALIGN', (-1, 0), (-2, 0), 'MIDDLE'), # 對(duì)齊 ('LINEBEFORE', (0, 0), (0, -1), 0.1, colors.grey), # 設(shè)置表格左邊線顏色為灰色,線寬為0.1 ('TEXTCOLOR', (0, 0), (-1, 0), colors.royalblue), # 設(shè)置表格內(nèi)文字顏色 ('TEXTCOLOR', (0, -1), (-1, -1), colors.red), # 設(shè)置表格內(nèi)文字顏色 ('GRID', (0, 0), (-1, -1), 0.5, colors.grey), # 設(shè)置表格框線為grey色,線寬為0.5 ] component_table = Table(dis_list, colWidths=colWidths,style=style) return component_table Style=getSampleStyleSheet() n = Style['Normal'] data = [[0,1,2,3,4,5,6,7], [00,11,22,33,44,55,66,77], [000,111,222,333,444,555,666,777], [0000,1111, 2222, 3333, 4444, 5555, 6666, 7777],] z = table_model(data) pdf = MyDocTemplate('ppff.pdf') pdf.multiBuild([Paragraph('Title',n),z])
4.添加邊框
from reportlab.graphics.shapes import Drawing
d = Drawing()
參數(shù)有:
d.width:邊框?qū)挾?/p>
d.heigth:邊框高度
d.background:邊框顏色
等等。
邊框中可使用add()添加文字,圖形等內(nèi)容
例:
在邊框中添加文字
from reportlab.graphics.shapes import Drawing, Rect from reportlab.graphics.charts.textlabels import Label def autoLegender( title=''): width = 448 height = 230 d = Drawing(width,height) lab = Label() lab.x = 220 #x和y是文字的位置坐標(biāo) lab.y = 210 lab.setText(title) # lab.fontName = 'song' #增加對(duì)中文字體的支持 lab.fontSize = 20 d.add(lab) d.background = Rect(0,0,width,height,strokeWidth=1,strokeColor="#868686",fillColor=None) #邊框顏色 return d l = autoLegender('hello') pdf=SimpleDocTemplate('ppff.pdf') pdf.multiBuild([l])
# **這種方法可以給邊框中的圖例添加顏色說明** def autoLegender(chart, categories=[], use_colors=[], title=''): width = 448 height = 230 d = Drawing(width,height) lab = Label() lab.x = 220 #x和y是title文字的位置坐標(biāo) lab.y = 210 lab.setText(title) # lab.fontName = 'song' #增加對(duì)中文字體的支持 lab.fontSize = 20 d.add(lab) d.background = Rect(0,0,width,height,strokeWidth=1,strokeColor="#868686",fillColor=None) #邊框顏色 d.add(chart) #顏色圖例說明等 leg = Legend() leg.x = 500 # 說明的x軸坐標(biāo) leg.y = 0 # 說明的y軸坐標(biāo) leg.boxAnchor = 'se' # leg.strokeWidth = 4 leg.strokeColor = None leg.subCols[1].align = 'right' leg.columnMaximum = 10 # 圖例說明一列最多顯示的個(gè)數(shù) # leg.fontName = 'song' leg.alignment = 'right' leg.colorNamePairs = zip(use_colors, tuple(categories)) #增加顏色說明 d.add(leg) return d
5.餅狀圖
餅圖需要添加在邊框中
from reportlab.lib import colors from reportlab.platypus import SimpleDocTemplate from reportlab.graphics.shapes import Drawing, Rect from reportlab.graphics.charts.textlabels import Label from reportlab.graphics.charts.piecharts import Pie def autoLegender( chart,title=''): width = 448 height = 230 d = Drawing(width,height) lab = Label() lab.x = 220 #x和y是文字的位置坐標(biāo) lab.y = 210 lab.setText(title) # lab.fontName = 'song' #增加對(duì)中文字體的支持 lab.fontSize = 20 d.add(lab) d.background = Rect(0,0,width,height,strokeWidth=1,strokeColor="#868686",fillColor=None) #邊框顏色 d.add(chart) return d def draw_pie(data=[], labels=[], use_colors=[], width=360,): '''更多屬性請(qǐng)查詢r(jià)eportlab.graphics.charts.piecharts.WedgeProperties''' pie = Pie() pie.x = 60 # x,y餅圖在框中的坐標(biāo) pie.y = 20 pie.slices.label_boxStrokeColor = colors.white #標(biāo)簽邊框的顏色 pie.data = data # 餅圖上的數(shù)據(jù) pie.labels = labels # 數(shù)據(jù)的標(biāo)簽 pie.simpleLabels = 0 # 0 標(biāo)簽在標(biāo)注線的右側(cè);1 在線上邊 pie.sameRadii = 1 # 0 餅圖是橢圓;1 餅圖是圓形 pie.slices.strokeColor = colors.red # 圓餅的邊界顏色 pie.strokeWidth=1 # 圓餅周圍空白區(qū)域的寬度 pie.strokeColor= colors.white # 整體餅圖邊界的顏色 pie.slices.label_pointer_piePad = 10 # 圓餅和標(biāo)簽的距離 pie.slices.label_pointer_edgePad = 25 # 標(biāo)簽和外邊框的距離 pie.width = width pie.direction = 'clockwise' pie.pointerLabelMode = 'LeftRight' # for i in range(len(labels)): # pie.slices[i].fontName = 'song' #設(shè)置中文 for i, col in enumerate(use_colors): pie.slices[i].fillColor = col return pie data = [10,9,8,7,6,5,4,3,2,1] labs = ['0000000','1111111','2222222','3333333','4444444', '5555555','6666666','7777777','8888888','9999999'] color = [HexColor("#696969"),HexColor("#A9A9A9"),HexColor("#D8BFD8"), HexColor("#DCDCDC"),HexColor('#E6E6FA'),HexColor("#B0C4DE"), HexColor("#778899"),HexColor('#B0C4DE'),HexColor("#6495ED"), HexColor("#483D8B") ] z = autoLegender(draw_pie(data,labs,color),labs,color) pdf=SimpleDocTemplate('ppff.pdf') pdf.multiBuild([z])
6.柱狀圖
柱狀圖需要添加在邊框中
from reportlab.graphics.charts.barcharts import VerticalBarChart from reportlab.lib.colors import HexColor def draw_bar_chart(min, max, x_list, data=[()], x_label_angle=0, bar_color=HexColor("#7BB8E7"), height=125, width=280): ''' :param min: 設(shè)置y軸的最小值 :param max: 設(shè)置y軸的最大值 :param x_list: x軸上的標(biāo)簽 :param data: y軸對(duì)應(yīng)標(biāo)簽的值 :param x_label_angle: x軸上標(biāo)簽的傾斜角度 :param bar_color: 柱的顏色 可以是含有多種顏色的列表 :param height: 柱狀圖的高度 :param width: 柱狀圖的寬度 :return: ''' bc = VerticalBarChart() bc.x = 50 # x和y是柱狀圖在框中的坐標(biāo) bc.y = 50 bc.height = height # 柱狀圖的高度 bc.width = width # 柱狀圖的寬度 bc.data = data for j in xrange(len(x_list)): setattr(bc.bars[j], 'fillColor', bar_color) # bar_color若含有多種顏色在這里分配bar_color[j] # 調(diào)整step minv = min * 0.5 maxv = max * 1.5 maxAxis = int(height/10) # 向上取整 minStep = int((maxv-minv+maxAxis-1)/maxAxis) bc.valueAxis.valueMin = min * 0.5 #設(shè)置y軸的最小值 bc.valueAxis.valueMax = max * 1.5 #設(shè)置y軸的最大值 bc.valueAxis.valueStep = (max-min)/4 #設(shè)置y軸的最小度量單位 if bc.valueAxis.valueStep < minStep: bc.valueAxis.valueStep = minStep if bc.valueAxis.valueStep == 0: bc.valueAxis.valueStep = 1 bc.categoryAxis.labels.boxAnchor = 'ne' # x軸下方標(biāo)簽坐標(biāo)的開口方向 bc.categoryAxis.labels.dx = -5 # x和y是x軸下方的標(biāo)簽距離x軸遠(yuǎn)近的坐標(biāo) bc.categoryAxis.labels.dy = -5 bc.categoryAxis.labels.angle = x_label_angle # x軸上描述文字的傾斜角度 # bc.categoryAxis.labels.fontName = 'song' x_real_list = [] if len(x_list) > 10: for i in range(len(x_list)): tmp = '' if i%5 != 0 else x_list[i] x_real_list.append(tmp) else: x_real_list = x_list bc.categoryAxis.categoryNames = x_real_list return bc z = autoLegender(draw_bar_chart(100, 300, ['a', 'b', 'c'], [(100, 200, 120)])) pdf=SimpleDocTemplate('ppff.pdf') pdf.multiBuild([z])
6.累加柱狀圖
def draw_2bar_chart(min, max, x_list, data=[()],array=[()], x_label_angle=0,bar_color=[],height=125, width=280): ''' :param min: 設(shè)置y軸的最小值 :param max: 設(shè)置y軸的最大值 :param x_list: x軸上的標(biāo)簽 :param data: y軸對(duì)應(yīng)標(biāo)簽的值 :param x_label_angle: x軸上標(biāo)簽的傾斜角度 :param bar_color: 柱的顏色 可以是含有多種顏色的列表 :param height: 柱狀圖的高度 :param width: 柱狀圖的寬度 :return: ''' bc = VerticalBarChart() bc.x = 50 # x和y是柱狀圖在框中的坐標(biāo) bc.y = 50 bc.height = height # 柱狀圖的高度 bc.width = width # 柱狀圖的寬度 bc.data = data # 圖形柱上標(biāo)注文字 bc.barLabels.nudge = -5 # 文字在圖形柱的上下位置 bc.barLabelArray = array # 要添加的文字 bc.barLabelFormat = 'values' for j in xrange(len(data)): setattr(bc.bars[j], 'fillColor', bar_color[j]) # bar_color若含有多種顏色在這里分配bar_color[j] # 調(diào)整step # minv = min * 0.5 minv = 0 maxv = max * 1.5 maxAxis = int(height/10) # 向上取整 minStep = int((maxv-minv+maxAxis-1)/maxAxis) bc.valueAxis.valueMin =0 #設(shè)置y軸的最小值 bc.valueAxis.valueMax = max * 1.5 #設(shè)置y軸的最大值 bc.valueAxis.valueStep = (max-min)/4 #設(shè)置y軸的最小度量單位 if bc.valueAxis.valueStep < minStep: bc.valueAxis.valueStep = minStep if bc.valueAxis.valueStep == 0: bc.valueAxis.valueStep = 1 bc.categoryAxis.labels.boxAnchor = 'ne' # x軸下方標(biāo)簽坐標(biāo)的開口方向 bc.categoryAxis.labels.dx = -5 # x和y是x軸下方的標(biāo)簽距離x軸遠(yuǎn)近的坐標(biāo) bc.categoryAxis.labels.dy = -5 bc.categoryAxis.labels.angle = x_label_angle # x軸上描述文字的傾斜角度 # bc.categoryAxis.labels.fontName = 'song' bc.categoryAxis.style = 'stacked' x_real_list = [] if len(x_list) > 10: for i in range(len(x_list)): tmp = '' if i%5 != 0 else x_list[i] x_real_list.append(tmp) else: x_real_list = x_list bc.categoryAxis.categoryNames = x_real_list return bc # 制柱狀圖 Style=getSampleStyleSheet() n = Style['Normal'] my_color = [HexColor('#E13C3C'),HexColor('#BE0000')] z = autoLegender(draw_2bar_chart(100, 300, ['a', 'b', 'c'], [(100, 200, 120),(150, 50, 130)], bar_color=my_color, array=[['100','200','120'],['150','50','130']] ), categories=['first','last'], use_colors=my_color ) pdf = MyDocTemplate('ppff.pdf') pdf.multiBuild([Paragraph('Title',n),z])
7.添加頁眉
添加頁眉需要我們自定義模版
from reportlab.platypus.doctemplate import BaseDocTemplate, Frame from reportlab.lib.units import cm from reportlab.platypus import PageTemplate from reportlab.lib.styles import getSampleStyleSheet import os def myMainPageFrame(canvas, doc): # 全局應(yīng)用 "The page frame used for all PDF documents." canvas.saveState() canvas.setFont('Times-Roman', 12) pageNumber = canvas.getPageNumber() if pageNumber > 0: pic_yemei = os.path.join(os.path.dirname(__file__),'yemei01.jpg') # 頁眉圖片 pic_line_file = os.path.join(os.path.dirname(__file__),'line.jpg') # 頁眉線 canvas.drawImage(pic_yemei, 75, 795, width=100,height=25) canvas.drawImage(pic_line_file, 75, 780, width=450, height=15) canvas.drawString(10*cm, cm, str(pageNumber)) canvas.restoreState() class MyDocTemplate(BaseDocTemplate): # 自定義模版類 "The document template used for all PDF documents." _invalidInitArgs = ('pageTemplates',) def __init__(self, filename, **kw): frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1') self.allowSplitting = 0 BaseDocTemplate.__init__(self, filename, **kw) template = PageTemplate('normal', [frame1], myMainPageFrame) self.addPageTemplates(template) # 綁定全局應(yīng)用 Style=getSampleStyleSheet() n = Style['Normal'] z = autoLegender(draw_bar_chart(100, 300, ['a', 'b', 'c'], [(100, 200, 120)])) pdf = MyDocTemplate('ppff.pdf') pdf.multiBuild([Paragraph('Title',n),z])
到此這篇關(guān)于python中的Reportlab模塊的文章就介紹到這了,更多相關(guān)python Reportlab內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)合成多張圖片到PDF格式
在日常生活中,經(jīng)常會(huì)遇到需要提交身份證正反面證明資料的情況,而且這些網(wǎng)站大部分只接受pdf格式,這時(shí)候我們就需要把身份證正反面兩張圖片合成為一個(gè)pdf文件。本文將為大家提供用Python實(shí)現(xiàn)這一要求的方法,需要的可以參考一下2022-02-02Python并發(fā)編程之進(jìn)程間通信原理及實(shí)現(xiàn)解析
這篇文章主要為大家介紹了Python并發(fā)編程之進(jìn)程間通信原理及實(shí)現(xiàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01python之matplotlib學(xué)習(xí)繪制動(dòng)態(tài)更新圖實(shí)例代碼
這篇文章主要介紹了python之matplotlib學(xué)習(xí)繪制動(dòng)態(tài)更新圖實(shí)例代碼,文中涉及具體實(shí)現(xiàn)代碼,演示效果及運(yùn)行時(shí)出現(xiàn)的問題分析等相關(guān)內(nèi)容,小編覺得還是挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下2018-01-01Python之NumPy(axis=0 與axis=1)區(qū)分詳解
這篇文章主要介紹了Python之NumPy(axis=0 與axis=1)區(qū)分詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Python Pandas Dataframe.describe()使用及代碼實(shí)例
這篇文章主要介紹了Python Pandas Dataframe.describe()使用及代碼實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09pytorch實(shí)現(xiàn)textCNN的具體操作
這篇文章主要介紹了pytorch實(shí)現(xiàn)textCNN的具體操作流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05