使用reportlab生成中文表格pdf

http://www.oschina.net/code/snippet_267866_20400


#coding:utf-8
from reportlab.lib.styles import getSampleStyleSheet,ParagraphStyle
from reportlab.platypus import *
from reportlab.lib.units import inch
from reportlab.lib.enums import TA_JUSTIFY

import reportlab.rl_config
reportlab.rl_config.warnOnMissingFontGlyphs = 0
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
#pdfmetrics.registerFont(TTFont('song', '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc'))
pdfmetrics.registerFont(TTFont('hei', '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc'))

from reportlab.lib import fonts,colors
#fonts.addMapping('song', 0, 0, 'song')
#fonts.addMapping('song', 0, 1, 'song')
fonts.addMapping('hei', 0, 0, 'hei')
fonts.addMapping('hei', 0, 1, 'hei')

stylesheet=getSampleStyleSheet()
elements = []

doc = SimpleDocTemplate('test.pdf')


elements.append(Paragraph('<font name="hei">学 生 成 绩 单</font>',stylesheet['Title']))
elements.append(Spacer(1,12))

stylesheet.add(ParagraphStyle(name='Justify',alignment=TA_JUSTIFY))
stylesheet['Justify'].fontName = 'hei'

elements.append(flowables.Preformatted('课程名称:_____________________    		                               主讲教师签名:_____________________' ,stylesheet['Justify']))
elements.append(Spacer(1,12))


data = []
data.append(['学号','姓名','成绩'])

import MySQLdb

conn = MySQLdb.connect(host="localhost",user="root",passwd="root",db="electsys",charset="utf8")
cur = conn.cursor()
sql = 'select s_no,s_name,s_score from student'
cur.execute(sql)
result = cur.fetchall()
for l in result:
    data.append(l)

#ts = [('ALIGN',(0,0),(-1,-1),'CENTER'),('FONT', (0,0), (-1,-1), 'hei')]
ts=[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),('BOX', (0,0), (-1,-1), 0.25, colors.black),('FONT', (0,0), (-1,-1), 'hei')]
table = Table(data, 2.1*inch, 0.24*inch, ts)
elements.append(table)

doc.build(elements)


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值