Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How python uses reportlab to generate pdf

2025-02-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how python uses reportlab to generate pdf". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how python uses reportlab to generate pdf" can help you solve the problem.

Intro

When you encounter business such as exporting statistical reports in a project, you usually need pdf format. The more famous one in python is reportlab.

Here through a few small demo quick demonstration of commonly used api. All function point source code is in use.

In a word: similar to css, it is constantly setting style for everything, and then binding style to content.

Function point

Generate

File: first SimpleDocTemplate ('xxx.pdf'), then build

Stream file: first io.BytesIO () to generate a handle, and then the same

Graph LinePlot

Pie chart Pie

Literal Paragraph

FontSize font size recommended 14

Bold xxx uses the way of html, and the font is realized automatically.

FirstLineIndent first line indentation recommendation 2 * fontSize

Leading line spacing is recommended for 1.5 * fontSize

FontName will become ■ by default.

Download at least 2 [regular] [bold] .ttf files

Registration font pdfmetrics.registerFont [regular] Please use the original name to facilitate bold implementation

Register font library registerFontFamily ("HanSans", normal= "HanSans", bold= "HanSans-Bold")

Other api fumbles on their own, but basically can not do without the concept of css. The official website does not have the md mode of regular documentation, but is completely written in pdf, and players need to look it up in pdf like a dictionary.

Preview

Complete code import osfrom reportlab.graphics.charts.lineplots import LinePlotfrom reportlab.graphics.charts.piecharts import Piefrom reportlab.graphics.shapes import Drawingfrom reportlab.lib import colorsfrom reportlab.lib.styles import ParagraphStylefrom reportlab.pdfbase import pdfmetricsfrom reportlab.pdfbase.pdfmetrics import registerFontFamilyfrom reportlab.pdfbase.ttfonts import TTFontfrom reportlab.platypus import Paragraphhome = os.path.expanduser ("~") try: pdfmetrics.registerFont (TTFont ("HanSans", f "{home} / .fonts/SourceHanSansCN-Normal.ttf") pdfmetrics.registerFont (TTFont ("HanSans-Bold") F "{home} / .fonts/SourceHanSansCN-Bold.ttf") registerFontFamily ("HanSans", normal= "HanSans", bold= "HanSans-Bold") FONT_NAME = "HanSans" except: FONT_NAME = "Helvetica" class MyCSS: H4 = ParagraphStyle (name= "h4", fontName=FONT_NAME, fontSize=14, leading=21, alignment=1) p = ParagraphStyle (name= "p", fontName=FONT_NAME, fontSize=12, leading=18, firstLineIndent=24) class PiiPdf: @ classmethod def doH3 (cls, text: str): return Paragraph (text MyCSS.h4) @ classmethod def doP (cls, text: str): return Paragraph (text, MyCSS.p) @ classmethod def doLine (cls): drawing = Drawing Line = LinePlot () line.x = 50 line.y = 50 line.height = 125 line.width = 300 line.lines [0] .strokeColor = colors.blue line.lines [1] .strokeColor = colors.red line.lines [2] .strokeColor = colors.green line.data = [(0,50), (100,100), (200,200), (250,210), (300,300) Drawing.add (line) return drawing @ classmethod def doChart (cls, data): drawing = Drawing (width=500, height=200) pie = Pie () pie.x = 150 pie.y = 65 pie.sideLabels = False pie.labels = [letter for letter in "abcdefg"] pie.data = data # list (range (15,105) Pie.slices.strokeWidth = 0.5drawing.add (pie) return drawing usage scenario 1: generate the file doc = SimpleDocTemplate ("Hello.pdf") p = PiiPdf () doc.build ([p.doH3 ("Water pump Energy consumption report"), p.doH3 ("2021.12.1 ~ 2021.12.31"), p.doP ("access to the Energy consumption Management system xx set in this month X pump. ") , p.doP ("the maximum total power xx kW of this month, an increase of xx% over the previous month, and an increase of xx% of the average power xx kW; over the previous month.") , p.doP ("power consumption trend chart:"), p.doLine (), p.doP ("Total energy consumption xxx kWh this month, an increase of xx% over the previous month.") , p.doP ("Energy consumption Statistics of Water pumps:"), p.doChart (list (range (15,105,20)), p.doP ("the pump with the highest energy consumption is xxx, which increased by xxx kWh,xx% over the previous month.") ]) use the scene 2:web (flask) @ Controller.get ("/ api/pdf") def api_hub_energy_pdf (): buffer = io.BytesIO () # highlight an io doc = SimpleDocTemplate (buffer) p = PiiPdf () doc.build ([p.doH3 (") 2021.12.1 ~ 2021.12.31 ") ]) buffer.seek (0) return Response (return buffer in # io form, mimetype= "application/pdf", headers= {"Content-disposition": "inline" Filename=test.pdf "},) this is the end of the introduction to" how python uses reportlab to generate pdf ". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report