2016-05-12 7 views
-1

Ich versuche, eine PDF-Datei anstelle einer Textdatei für ein Programm zu erstellen, das ich erstellt habe.PDF-Generierung in Python

Mein Problem ist, ich habe ein Reportlab geschaut und es scheint zu kompliziert für das, was ich brauche, als alles, was ich will, ist im Wesentlichen print zu einer PDF statt einer Textdatei.

aktuellen Code-Test, es funktioniert, aber ich bin verwirrt über Positionierung und alle Linien des Ende der Seite laufen, könnte mir bitte jemand beraten, wie Positionierung arbeitet mit reportlab

from reportlab.pdfgen import canvas 
def genText(): 
    text =["Call me Ishmael.", 
     "Some years ago- never mind how long precisely- having little or no money in my purse,", 
     "and nothing particular to interest me on shore,", 
     "I thought I would sail about a little and see the watery part of the world."] 
    return text 

def testText(page,text): 

    from reportlab.lib.units import inch 

    textobject = page.beginText() 
    textobject.setTextOrigin(inch, 2.5*inch) 
    textobject.setFont("Helvetica-Oblique", 14) 
    for line in text: 
     textobject.textLine(line) 

    page.drawText(textobject)  

page = canvas.Canvas("JIMTEST.pdf") 
text = genText() 
testText(page, text) 
page.showPage() 
page.save() 

Daten, die ist tatsächlich ausgegeben durch mein Programm:

--------------------------------------------------------- 
Milk Company: Bobbys Milk 
Haulier: Jims Hauliers 
Truck: T55JHH 

Driver: 123  Route: 852 
    Joe Bloggs 

Everyday Collection 

MilkType: Ordinary 

--------------------------------------------------------- 
Last TankWash 

Start Time: 2016/03/31 13:30:32 
Finsished: 14:21:03 
Litres: 9451 
Temperature: 70.0 deg_C 

    770500 CREAMERY 

--------------------------------------------------------- 
Locn     Litres 

    770083 Wyrill 
Coll  1643 2.0 deg_C smp 143 
2016/04/01 06:40:28 

    770084 Foster 
Coll  2242 1.0 deg_C smp 28 
2016/04/01 07:17:57 

    770080 Dugdale 
Coll  8237 4.0 deg_C smp 49 
2016/04/01 08:02:39 

    770086 Cragg 
Coll  4591 1.7 deg_C smp 68 
2016/04/01 09:00:17 

    770051 D & S Spence 
Coll  2868 3.7 deg_C smp 83 
2016/04/01 10:06:11 

    770500 CREAMERY 
delyFZ -19581 
Tank#  0 Ower# 3805 
2016/04/01 11:14:11 


--------------------------------------------------------- 
Milk Collected: 19581 
Milk OnBoard: 0 
--------------------------------------------------------- 
Estimated Print Time at: 2016/04/01 11:14:16 

Dank

+0

Was ist die Frage hier? – Selcuk

+0

@Selcuk Sorry, ich brauche Hilfe, wie die Positionierung im Berichtslabor funktioniert und wie man erkennt, ob eine Zeile von der Seite – Jim

+1

Google wie Positionierung in Reportlab verwendet wird, könnte etwas wie folgt kommen https://dzone.com/articles/reportlab-mixing-fixed-content – Eliethesaiyan

Antwort

1

ich sugggest Sie am Schnabeltier Teil reportlab aussehen.

Der Absatz entspricht in HTML weitgehend einem DIV-Container und wird automatisch am Ende des Dokuments umbrochen.

from reportlab.lib.pagesizes import A4 
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer 
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle 

def genText(): 
    text = ["Call me Ishmael.", 
     "Some years ago- never mind how long precisely- having little or no money in my purse,", 
     "and nothing particular to interest me on shore,", 
     "I thought I would sail about a little and see the watery part of the world."] 
    return text 

styles = getSampleStyleSheet() 
doc = SimpleDocTemplate("my_doc.pdf", pagesize=A4) 
Story=[] 
text = genText() 
for t in text: 
    Story.append(Paragraph(t, styles["Normal"])) 
Story.append(Spacer(10, 10)) 
t = "lorem ipsum " * 100 
Story.append(Paragraph(t, styles["Normal"])) 
doc.build(Story) 

Of couse Sie auch Leinwand und Schnabeltier mischen können - dies ist nur ein minimales Beispiel um Ihnen zu gehen.

Um Ihre Elemente zu positionieren, empfehle ich Ihnen, das Schnabeltier Table zu betrachten.