2016-09-01 19 views
2

所以,一段時間以來,我一直在努力與這一個。我知道有很多類似的問題和很好的答案,而且我已經嘗試了這些答案,但是我所用的代碼基本上反映了給出的答案。VALUE在reportlab TableStyle顯然沒有效果

我正在編寫代碼來自動生成工作表的匹配練習。所有這些信息應該放在一個表格中。文本應該全部對齊到單元格的頂部。

這是我現在有:

from reportlab.lib.pagesizes import A4 
from reportlab.platypus import SimpleDocTemplate, Paragraph, Table,   TableStyle 
from reportlab.lib.styles import getSampleStyleSheet 
from reportlab.lib.units import cm 

document = [] 
doc = SimpleDocTemplate('example.pdf', pagesize=A4, rightMargin=72, leftMargin=72, topMargin=72) 
styles = getSampleStyleSheet() 

definitions = [] 
i, a = 1, 65 
table = [] 
for x in range(1, 10): 
    line = [] 
    line.append(Paragraph(str(i), styles['BodyText'])) 
    line.append(Paragraph('Vocabulary', styles['BodyText'])) 
    line.append(Paragraph(chr(a), styles['BodyText'])) 
    line.append(Paragraph('Often a multi-line definition of the vocabulary. But then, sometimes something short and sweet.', styles['BodyText'])) 
    table.append(line) 
    i += 1 
    a += 1 

t = Table(table, colWidths=(1*cm, 4*cm, 1*cm, None)) 
t.setStyle(TableStyle([ 
    ('VALIGN', (1, 1), (-1, -1), 'TOP') 
])) 

document.append(t) 
doc.build(document) 

我是什麼俯瞰?

+0

愛你的個人資料中的故事,我希望我的老師回到高中時會有同樣的態度! – B8vrede

+0

謝謝!我不得不回去再讀一遍,但仍然如此。我不會說謊:今天它給了我幾根灰頭髮。 –

回答

3

問題是您索引TableStyle的方式。 Reportlab中的索引在第一行第一列的(0, 0)處開始。所以在你的情況下,(1, 1)只適用於第一行下方第一行和右側第一列的所有樣式。

正確的方法是使用:

('VALIGN', (0, 0), (-1, -1), 'TOP') 

這將造型適用於所有細胞中Table

+1

感謝患者回答。這很好。我不知道爲什麼我自己看不到它,但我很高興你做到了。 –