Stumbled on a giant cow artificial intelligence course, could not help but share to everyone. Tutorial is not only a zero-based, user-friendly, and very humorous, like watching a fiction! I think too much bad, so to others. Tutorial links: https://www.cbedai.net/qtlyx
Many times, we want to automate reporting, but usually need to use Chinese, but reportlab natural does not support Chinese, so we need to support him, and approach is very simple.
1, download fonts
For example SimHei.TTF download the font file, Baidu can, then put it reportlab installation package following font folder.
For example, the author's path is this.
2, what font to use pdf
pdfmetrics.registerFont(TTFont('SimHei', 'SimHei.TTF'))
such as:
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
pdfmetrics.registerFont(TTFont('SimHei', 'SimHei.TTF'))
def hello(c):
c.drawString(100,100,"世界你好")
c = canvas.Canvas("hello.pdf")
c.setFont('SimHei',12)
hello(c)
c.showPage()
c.save()
The Chinese will generate a pdf after this operation.