網站首頁 編程語言 正文
Intro
項目中遇到需要 導出統計報表
等業務時,通常需要 pdf
格式。python
中比較有名的就是 reportlab
。
這邊通過幾個小 demo 快速演示常用 api。所有功能點 源碼
都在 使用場景
。
一句話了解:跟 css 差不多,就是不斷地對每樣東西設置 style,然后把 style 和內容綁定。
功能點
生成
文件: 先 SimpleDocTemplate(‘xxx.pdf’),然后 build
流文件:先 io.BytesIO() 生成句柄,然后同理
曲線圖 LinePlot
餅圖 Pie
文字 Paragraph
fontSize 字體大小 推薦 14
加粗 <b>xxx</b> 使用的是 html 的方式,字體自動實現
firstLineIndent 首行縮進 推薦 2 * fontSize
leading 行間距 推薦 1.5 * fontSize
fontName 默認中文會變成 ■
下載 .ttf 文件 至少2個 【常規】【加粗】
注冊字體 pdfmetrics.registerFont 【常規】請用原名,方便加粗的實現
注冊字體庫 registerFontFamily(“HanSans”, normal=“HanSans”, bold=“HanSans-Bold”)
其他 api 自行摸索,但基本離不開 css 那種理念。官網并沒有常規文檔的那種 md 模式,而是完全寫在了 pdf 里,玩家需要自己去 pdf 里像查字典一樣去找。
預覽
完整代碼
import os from reportlab.graphics.charts.lineplots import LinePlot from reportlab.graphics.charts.piecharts import Pie from reportlab.graphics.shapes import Drawing from reportlab.lib import colors from reportlab.lib.styles import ParagraphStyle from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.pdfmetrics import registerFontFamily from reportlab.pdfbase.ttfonts import TTFont from reportlab.platypus import Paragraph home = 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: h3 = ParagraphStyle(name="h3", 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.h3) @classmethod def doP(cls, text: str): return Paragraph(text, MyCSS.p) @classmethod def doLine(cls): drawing = Drawing(500, 220) 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), (400, 800))] 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, 15)) pie.slices.strokeWidth = 0.5 drawing.add(pie) return drawing
使用場景1:生成文件
doc = SimpleDocTemplate("Hello.pdf") p = PiiPdf() doc.build([ p.doH3("<b>水泵能源消耗簡報</b>"), p.doH3("<b>2021.12.1 ~ 2021.12.31</b>"), p.doP("該月接入能耗管理系統水泵系統 xx 套,水泵 x 臺。"), p.doP("本月最大總功率 xx kW,環比上月增加 xx %,平均功率 xx kW;環比上月增加 xx %。"), p.doP("功率消耗趨勢圖:"), p.doLine(), p.doP("本月總能耗 xxx kWh,環比上月增加 xx %。"), p.doP("分水泵能耗統計:"), p.doChart(list(range(15, 105, 20))), p.doP("其中能耗最高的水泵為:xxx, 環比上月增加 xxx kWh,xx %。"), ])
使用場景2:web(flask)
@Controller.get("/api/pdf") def api_hub_energy_pdf(): buffer = io.BytesIO() # 重點 起一個 io doc = SimpleDocTemplate(buffer) p = PiiPdf() doc.build([ p.doH3("<b>2021.12.1 ~ 2021.12.31</b>"), ]) buffer.seek(0) return Response( # io 形式返回 buffer, mimetype="application/pdf", headers={"Content-disposition": "inline; filename=test.pdf"}, )
總結
原文鏈接:https://blog.csdn.net/wolanx/article/details/122828185
相關推薦
- 2022-08-16 解決Django?cors跨域問題_python
- 2022-08-05 處理“URIError: URI malformed“的辦法
- 2022-11-16 詳解Python中range()與xrange()的區別_python
- 2022-06-10 C語言?模擬實現strlen函數詳解_C 語言
- 2022-09-17 Golang文件讀寫操作詳情_Golang
- 2023-03-28 Python表示當前時間的方法合集_python
- 2022-07-15 C語言函數棧幀的創建與銷毀原理圖解_C 語言
- 2022-07-11 Python標準庫uuid模塊(生成唯一標識)詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支