我想使用Python生成比较表like this。
alt text http://img714.imageshack.us/img714/5677/22862352.png
你能建议任何图书馆这样做吗?
答案 0 :(得分:1)
这肯定是TeX的输出。需要花费60分钟来学习足够的TeX(or, better yet, LaTeX)来吐出这样的表格。
答案 1 :(得分:0)
Reportlab有一些很棒的工具可用于创建和格式化表格。您的观点唯一的缺点是它输出到PDF,因此您需要一个中间步骤将输出转换为PNG。有关Reportlab表的更多信息,请查看Reportlab User Guide。
以下是我编写的一些代码,它们会创建与您的示例图像非常相似的代码:
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import Frame, Table
from reportlab.lib.units import mm
from reportlab.lib.colors import black
canvas = Canvas("table_test.pdf")
frame = Frame(10*mm, 10*mm, 190*mm, 280*mm)
# Some mock data for the table. The data must be supplied as a list of lists:
table_data = [['Datasets', 'COL1', 'COL2', 'COL3', 'COL4', 'COL5'],
['ROW0',
u'6.43 \xb1 4.72',
u'6.16 \xb1 3.83',
u'2.79 \xb1 5.43',
u'19.08 \xb1 1.12',
u'1.76 \xb1 4.88'],
['ROW1',
u'1.09 \xb1 2.42',
u'11.51 \xb1 1.29',
u'10.14 \xb1 0.76',
u'10.09 \xb1 0.63',
u'27.16 \xb1 3.88'],
['ROW2',
u'0.88 \xb1 5.83',
u'23.13 \xb1 4.54',
u'8.96 \xb1 4.94',
u'4.30 \xb1 0.30',
u'13.20 \xb1 1.31'],
['ROW3',
u'22.87 \xb1 2.22',
u'8.56 \xb1 0.91',
u'6.36 \xb1 1.69',
u'4.30 \xb1 4.74',
u'12.39 \xb1 3.49'],
['ROW4',
u'14.35 \xb1 5.04',
u'15.29 \xb1 2.17',
u'8.39 \xb1 3.05',
u'11.38 \xb1 3.02',
u'10.21 \xb1 4.23']]
table_style = [("BOX", (0, 0), (-1, -1), 0.5, black), # box around table
("LINEBELOW", (0, 0), (-1, 0), 0.5, black), # line under header
("LINEAFTER", (0, 0), (0, -1), 0.5, black), # vertical line
("LINEAFTER", (1, 0), (1, -1), 0.5, black), # vertical line
("LINEAFTER", (2, 0), (2, -1), 0.5, black), # vertical line
("LINEAFTER", (3, 0), (3, -1), 0.5, black), # vertical line
("LINEAFTER", (4, 0), (4, -1), 0.5, black), # vertical line
]
table = Table(table_data, style=table_style)
frame.add(table, canvas)
canvas.save()