2012-12-19 2 views
8

나는 문서에이 스타일을 정의하고 있습니다 : Reportlab 테이블에서 수직 공간을 결정하는 것은 무엇입니까?

styles.add(ParagraphStyle(name='Table Header', font ='Helvetica-Bold',fontSize=16, alignment=TA_CENTER)) 

내가 (제대로 감싸도록) 텍스트가 각 테이블의 맨 윗줄로 이동하는 단락을 정의하려면이를 사용

L2sub = [(Paragraph(L[0][0], styles['Table Header']))] 

report.append(Table(data,style=[ 
       ('GRID',(0,0),(len(topiclist)-1,-1),0.5,colors.grey), 
       ('FONT', (0,0),(len(topiclist)-1,0),'Helvetica-Bold',16), 
       ('FONT', (0,1),(len(topiclist)-1,1),'Helvetica-Bold',12), 
       ('ALIGN',(0,0),(-1,-1),'CENTER'), 
       ('VALIGN',(0,0),(-1,-1),'MIDDLE'), 
       ('SPAN',(0,0),(len(topiclist)-1,0)), 
       ])) 

내 질문은 :가 어디 settin 인 나는 테이블을 추가 할 때

나중에도 스타일을 정의 할 수있는 곳입니다 g는 첫 번째 행에있는 셀의 세로 높이를 정의합니다. 텍스트가 셀에 비해 너무 크거나 셀에 너무 낮게 설정되어있는 문제가 있지만 문제의 원인이나 해결 방법을 파악할 수 없습니다. 나는 두 가지 크기를 모두 바꿨으나 세포를 같은 높이가 아닌 다른 것으로 만들 수는 없습니다. 단락 대신 셀에 텍스트를 넣을 때 표 def'n이 제대로 작동했지만 단락으로 인해 문제가 발생했습니다.

+0

필자가 단락을 사용하지 않고 표 셀에 직접 텍스트를 넣으면 자동으로 줄 바꿈이 일어날 수 있다는 것을 이해했습니다. 단락은 자신의 논리를 래핑하는 데 작업 할 수있는 고정 된 공간이 필요합니다. 그렇다면 왜 단락을 사용하여 "올바르게 포장"합니까? –

+0

그건 내 경험이 아니 었습니다. 텍스트를 단락에 넣기 전까지는 줄 바꿈하지 않았기 때문에 테이블의 경계를 넘었습니다. 나는 그 다른 생각에서 그 아이디어를 얻었다. – DeltaG

답변

5

행 높이를 변경할 수있는 TableStyle의 설정이 있다고 생각하지 않습니다. colwidthsrowheights이 측정 값의 목록입니다 그래서 같은

Table(data, colwidths, rowheights) 

:

from reportlab.lib.units import inch 
from reportlab.lib.styles import getSampleStyleSheet 
from reportlab.platypus import Paragraph 
from reportlab.platypus import Table 
from reportlab.lib import colors 

# Creates a table with 2 columns, variable width 
colwidths = [2.5*inch, .8*inch] 

# Two rows with variable height 
rowheights = [.4*inch, .2*inch] 

table_style = [ 
    ('GRID', (0, 1), (-1, -1), 1, colors.black), 
    ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), 
    ('ALIGN', (1, 1), (1, -1), 'RIGHT') 
] 

style = getSampleStyleSheet() 

title_paragraph = Paragraph(
    "<font size=13><b>My Title Here</b></font>", 
    style["Normal"] 
) 
# Just filling in the first row 
data = [[title_paragraph, 'Random text string']] 

# Now we can create the table with our data, and column/row measurements 
table = Table(data, colwidths, rowheights) 

# Another way of setting table style, using the setStyle method. 
table.setStyle(tbl_style) 

report.append(table) 

colwidthsrowheights 무엇이든 측정으로 변경 될 수 측정은 새 Table 개체를 만들 때 주어집니다 당신은 내용에 맞게해야합니다. colwidths은 왼쪽에서 오른쪽으로 읽으며, rowheights은 위에서 아래로 읽습니다. 당신이 당신의 테이블의 모든 행이 동일한 높이로려고하고 있다는 것을 알고 있다면

,이 좋은 바로 가기를 사용할 수 있습니다

당신의 data 변수의 모든 행에 대해 당신에게 [.2*inch, .2*inch, ...] 같은 목록을 제공
rowheights = [.2*inch] * len(data) 

.