1. 程式人生 > 實用技巧 >reportlab中如何寫中文

reportlab中如何寫中文

https://www.jianshu.com/p/41284e4e25f5

https://github.com/StellarCN/scp_zh/blob/master/fonts/SimSun.ttf

from reportlab.lib.colors import blue
from reportlab.lib.pagesizes import LETTER,A4
from reportlab.lib.units import inch
from reportlab.pdfgen.canvas import Canvas

from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

pdfmetrics.registerFont(TTFont('SimSun', 'SimSun.ttf'))  #註冊字型

canvas = Canvas("./PythonScript/Financial Statements.pdf", pagesize=LETTER)
# Set font to Times New Roman with 12-point size
canvas.setFont("SimSun", 25)

# Draw blue text one inch from the left and ten
# inches from the bottom
canvas.setFillColor(blue)

canvas.drawString(4.25 * inch, 10 * inch, r"報表")
canvas.drawString(4.25 * inch, 9 * inch, r"Results")

# Save the PDF file
canvas.save()