2016-07-27 2 views
1

Я пытаюсь начать работу в reportlab. Я знаю, как добавлять строки, строки и т. Д. Теперь я хочу иметь возможность комбинировать статический текст с параграфами, а статический текст должен находиться в определенной позиции. Извините за ошибки отступов.Reportlab mix статический текст и параграфы

from reportlab.pdfgen import canvas 
from reportlab.lib.pagesizes import letter 
from reportlab.platypus import Image, SimpleDocTemplate, Paragraph, Spacer,  
from reportlab.rl_config import defaultPageSize 
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle 
from reportlab.lib.enums import TA_JUSTIFY 


PAGE_HEIGHT=defaultPageSize[1]; PAGE_WIDTH=defaultPageSize[0] 

Story = [] 

p = "This is a paragraph" 



last_name = "John" 
first_name = "Doe" 
dosuren = "02-03-2016" 



def generate_report(last_name, first_name, dosuren): 
    pdf_file_name = last_name + first_name + "_" + dosuren + ".pdf" 

    c= canvas.Canvas(pdf_file_name, pagesize=letter) 

    c.setFont('Times-Bold', 12,leading=None) 
    c.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, "REPORT") 

##### Static Text ############ 


c.setFont('Times-Bold', 12, leading=None) 
c.drawString(30, 320, "Subject INfo:") 

c.setFont('Times-Roman', 12, leading=None) 
c.drawString(30, 380, "Subject info2:") 

##Paragraph### 
styles = getSampleStyleSheet() 
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY)) 
Story.append(Paragraph(p, styles["Justify"])) 


c.showPage() 
c.save() 

ответ

0

Из Ваших требований ясно, что вы ищете PageTemplate, что позволяет первому сделать некоторые фиксированные элементы на странице, а затем добавить Paragraph «S и другие Flowable» S к нему.

Быстрый пример будет выглядеть следующим образом:

from reportlab.lib.styles import getSampleStyleSheet 
from reportlab.platypus import BaseDocTemplate, PageTemplate, Frame, Paragraph 

def draw_static(canvas, doc): 
    # Save the current settings 
    canvas.saveState() 

    # Draw the static stuff 
    canvas.setFont('Times-Bold', 12, leading=None) 
    canvas.drawCentredString(200, 200, "Static element") 

    # Restore setting to before function call 
    canvas.restoreState() 

# Set up a basic template 
doc = BaseDocTemplate('test.pdf') 

# Create a Frame for the Flowables (Paragraphs and such) 
frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height, id='normal') 

# Add the Frame to the template and tell the template to call draw_static for each page 
template = PageTemplate(id='test', frames=[frame], onPage=draw_static) 

# Add the template to the doc 
doc.addPageTemplates([template]) 

# All the default stuff for generating a document 
styles = getSampleStyleSheet() 
story = [] 

P = Paragraph('This is a paragraph', styles["Normal"]) 

story.append(P) 
doc.build(story) 
Смежные вопросы