2011-01-18 16 views
14

私はテーブルを使用していますが、私はflowableの位置を制御するためにキャンバスに描画します。これはpdfのテンプレートを持っているので、私はpyPDFとマージします。テーブルreportlabにテキストをラップしますか?

ラップはテーブルで行われますが、テキストは上向きで、下向きではありません。

cがキャンバス

コード

from reportlab.pdfgen import canvas 
from reportlab.lib.pagesizes import A4 
from reportlab.lib.styles import getSampleStyleSheet 
from reportlab.platypus import Paragraph, Table 
from reportlab.lib.units cm 

width, height = A4 
styles = getSampleStyleSheet() 

def coord(x, y, unit=1): 
    x, y = x * unit, height - y * unit 
    return x, y 

descrpcion = Paragraph('long paragraph', styles["Normal"]) 
partida = Paragraph('1', styles["Center"]) 
candidad = Paragraph('120', styles["Center"]) 
precio_unitario = Paragraph('$52.00', styles["right"]) 
precio_total = Paragraph('$6240.00', styles["right"]) 

data= [[partida, candidad, descrpcion, precio_unitario, precio_total]] 
table = Table(data, colWidths=[2.05 * cm, 2.7 * cm, 9.6 * cm, 
           2.65 * cm, 2.7 * cm]) 

c = canvas.Canvas(PDF, pagesize=A4) 
table.wrapOn(c, width, height) 
table.drawOn(c, *coord(1.8, 9.6, cm)) 
c.save() 

http://img600.imageshack.us/img600/3203/reportld.jpg

答えて

15

これは、テーブルの内容の依存["Normal"]テキストをスタイル["BodyText"]にラップしようとすることができます。これにより、指定したセルの幅に合わせてテキストを整列させることができます。 HTMLテキストの書式設定に似た書式を含めることもできます。

次に、TableStyleを使用して、表内のコンテンツの書式を設定します(たとえば、カラーテキスト、中央段落、行/列など)。

Iは、作業バージョン(例)に上記のコードを編集:

from reportlab.pdfgen import canvas 
from reportlab.lib.pagesizes import A4, cm 
from reportlab.lib.styles import getSampleStyleSheet 
from reportlab.platypus import Paragraph, Table, TableStyle 
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER 
from reportlab.lib import colors 

width, height = A4 
styles = getSampleStyleSheet() 
styleN = styles["BodyText"] 
styleN.alignment = TA_LEFT 
styleBH = styles["Normal"] 
styleBH.alignment = TA_CENTER 

def coord(x, y, unit=1): 
    x, y = x * unit, height - y * unit 
    return x, y 

# Headers 
hdescrpcion = Paragraph('''<b>descrpcion</b>''', styleBH) 
hpartida = Paragraph('''<b>partida</b>''', styleBH) 
hcandidad = Paragraph('''<b>candidad</b>''', styleBH) 
hprecio_unitario = Paragraph('''<b>precio_unitario</b>''', styleBH) 
hprecio_total = Paragraph('''<b>precio_total</b>''', styleBH) 

# Texts 
descrpcion = Paragraph('long paragraph', styleN) 
partida = Paragraph('1', styleN) 
candidad = Paragraph('120', styleN) 
precio_unitario = Paragraph('$52.00', styleN) 
precio_total = Paragraph('$6240.00', styleN) 

data= [[hdescrpcion, hcandidad,hcandidad, hprecio_unitario, hprecio_total], 
     [partida, candidad, descrpcion, precio_unitario, precio_total]] 

table = Table(data, colWidths=[2.05 * cm, 2.7 * cm, 5 * cm, 
           3* cm, 3 * cm]) 

table.setStyle(TableStyle([ 
         ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), 
         ('BOX', (0,0), (-1,-1), 0.25, colors.black), 
         ])) 

c = canvas.Canvas("a.pdf", pagesize=A4) 
table.wrapOn(c, width, height) 
table.drawOn(c, *coord(1.8, 9.6, cm)) 
c.save() 
+0

ありがとう、これは良い解決策ではなく、私の答えのようなハックです。 – Alquimista

+0

テーブルスタイル( 'VALIGN'、(0,0)、( - 1、-1)、 'TOP)へのいくつかのadittion – Alquimista

+0

喜んで助けてください。あまりにも悪い、reportlabは長いテーブルをサポートしていません、それ以外の場合は、素晴らしいPDFレポートの作成者でした。 –

0

私はポストスクリプトの参照が低く、左隅であることを知っています。私はPDFが同じであると推測していますので、y値から減算すると下になります。関数の開始と終了の "y"値を出力して、それらがどのように変化しているかを見て、文の長さに応じて "y"の値を調整します。関数はどのように「高さ」が何であるか知っていますか?私はReportLabを使用していますが、投稿するのが気になる場合は、具体的な例を参考にしてください。

+0

はい、私は[コード]、幅、高さ= A4 [/コード]座標が 、PDF内のデカルトのようなものであることを知っています – Alquimista

3

自動返信:

def coord(x, y, height, unit=1): 
    x, y = x * unit, height - y * unit 
    return x, y 

w, h = table.wrap(width, height) 
table.wrapOn(c, width, height) 
table.drawOn(c, *coord(ml - 0.05, y + 4.6, height - h, cm)) 

トリックである - 、Hはテーブルの高さである「高さH」と説明テキストが行った

関連する問題