網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Intro
項(xiàng)目中遇到需要 導(dǎo)出統(tǒng)計(jì)報(bào)表
等業(yè)務(wù)時(shí),通常需要 pdf
格式。python
中比較有名的就是 reportlab
。
這邊通過(guò)幾個(gè)小 demo 快速演示常用 api。所有功能點(diǎn) 源碼
都在 使用場(chǎng)景
。
一句話了解:跟 css 差不多,就是不斷地對(duì)每樣?xùn)|西設(shè)置 style,然后把 style 和內(nèi)容綁定。
功能點(diǎn)
生成
文件: 先 SimpleDocTemplate(‘xxx.pdf’),然后 build
流文件:先 io.BytesIO() 生成句柄,然后同理
曲線圖 LinePlot
餅圖 Pie
文字 Paragraph
fontSize 字體大小 推薦 14
加粗 <b>xxx</b> 使用的是 html 的方式,字體自動(dòng)實(shí)現(xiàn)
firstLineIndent 首行縮進(jìn) 推薦 2 * fontSize
leading 行間距 推薦 1.5 * fontSize
fontName 默認(rèn)中文會(huì)變成 ■
下載 .ttf 文件 至少2個(gè) 【常規(guī)】【加粗】
注冊(cè)字體 pdfmetrics.registerFont 【常規(guī)】請(qǐng)用原名,方便加粗的實(shí)現(xiàn)
注冊(cè)字體庫(kù) registerFontFamily(“HanSans”, normal=“HanSans”, bold=“HanSans-Bold”)
其他 api 自行摸索,但基本離不開 css 那種理念。官網(wǎng)并沒(méi)有常規(guī)文檔的那種 md 模式,而是完全寫在了 pdf 里,玩家需要自己去 pdf 里像查字典一樣去找。
預(yù)覽
完整代碼
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
使用場(chǎng)景1:生成文件
doc = SimpleDocTemplate("Hello.pdf") p = PiiPdf() doc.build([ p.doH3("<b>水泵能源消耗簡(jiǎn)報(bào)</b>"), p.doH3("<b>2021.12.1 ~ 2021.12.31</b>"), p.doP("該月接入能耗管理系統(tǒng)水泵系統(tǒng) xx 套,水泵 x 臺(tái)。"), p.doP("本月最大總功率 xx kW,環(huán)比上月增加 xx %,平均功率 xx kW;環(huán)比上月增加 xx %。"), p.doP("功率消耗趨勢(shì)圖:"), p.doLine(), p.doP("本月總能耗 xxx kWh,環(huán)比上月增加 xx %。"), p.doP("分水泵能耗統(tǒng)計(jì):"), p.doChart(list(range(15, 105, 20))), p.doP("其中能耗最高的水泵為:xxx, 環(huán)比上月增加 xxx kWh,xx %。"), ])
使用場(chǎng)景2:web(flask)
@Controller.get("/api/pdf") def api_hub_energy_pdf(): buffer = io.BytesIO() # 重點(diǎn) 起一個(gè) 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"}, )
總結(jié)
原文鏈接:https://blog.csdn.net/wolanx/article/details/122828185
相關(guān)推薦
- 2022-04-27 一文教你如何封裝安全的go_Golang
- 2022-05-18 Python?Pandas?刪除列操作_python
- 2023-02-17 pytorch?cuda安裝報(bào)錯(cuò)的解決方法_python
- 2022-07-21 vscode代碼保存時(shí)自動(dòng)格式化
- 2022-06-01 Python全角與半角之間相互轉(zhuǎn)換的方法總結(jié)_python
- 2023-02-10 android?原生安全音量配置邏輯設(shè)計(jì)詳解_Android
- 2023-07-30 el-table自定義合并行或列
- 2022-06-08 并發(fā)編程CountdownLatch結(jié)合線程池實(shí)現(xiàn)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支