How to create a PDF from data from a Dictionary extracted from a .txt file (Python)

0

I have dict() with string data extracted from a arquivo.txt .

dic = {}
dic[valor[0]] = valor[1]
print(dic)
>>> dic = {'Almir': 44, 'Ana': 36 ....}

I would like to save organized into PDF as follows:

nome : 8585
nome : 83838
    
asked by anonymous 16.11.2017 / 21:00

2 answers

0

As suggested by @cryptotux , use the reportlab .

To generate the PDF through a dictionary, where each word will be in a line, do:

import os
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch

# Aqui vem o código de criação do dicionário 'dic'

c = canvas.Canvas("arquivo.pdf")

# Move a origem do cursor para a parte superior esquerda
c.translate(inch,inch)

# Inicia um objeto texto limitando a área para que linhas 
# muito grandes, não ultrapassem a margem.
textobject = c.beginText(0, 650)
textobject.setFont("Helvetica-Oblique", 14)

# Percorrendo o dicionário definido anteriormente
for key, value in dic.items():
    textobject.textLine(key + ' : ' + value)

c.drawText(textobject)

c.showPage()
c.save()

os.system('arquivo.pdf')
    
23.11.2017 / 16:29
2

Install reportlab :

pip install reportlab

Creating PDF and saving:

from reportlab.pdfgen import canvas

c = canvas.Canvas("arquivo.pdf")
#(x, y, string)
c.drawString(0,0,str(dic))
c.save()

If you want to take a look at the documentation: link

    
19.11.2017 / 19:29