Estoy usando reportlab para generar pdf. Tengo esta función en django rest framework, en la cual tengo un diccionario llamado datos, estoy haciendo un for para reccorrer esa data y mostrar su clave y valor en el pdf, para ello hago esto:
@api_view()
def hello_world(request):
todos =
[
{
primer_apellido : Castro,
segundo_apellido : Mendieta,
primer_nombre : José,
segundo_nombre : Raúl,
genero : masculino,
lugar_de_nacimiento : Cuenca - Azuay,
fecha_de_nacimiento : 1967-10-20,
provincia : Loja,
canton : Cuenca,
ciudad : Loja,
parroquia : El Vecino,
direccion : Turunuma Alto. Urb. Elite. Casa A1.,
telefono_institucion : 13213213,
email_principal : jrcastro@utpl.edu.ec
}
]
buffer = io.BytesIO()
# Create the PDF object, using the buffer as its "file."
p = canvas.Canvas(buffer)
# # Draw things on the PDF. Here's where the PDF generation happens.
# # See the ReportLab documentation for the full list of functionality.
from reportlab.lib import colors
from reportlab.lib.units import inch
y = x = 0
dy = inch*3/4.0
dx = inch*5.5/5
w = h = dy/2
rdx = (dx-w)/2
rdy = h/5.0
texty = h+2*rdy
for todo in todos:
p.drawString(x+dx/2, y+texty, todo, todos[todo])
x = x+dx
y = y + dy
x = 0
# Close the PDF object cleanly, and we're done.
p.showPage()
p.save()
# FileResponse sets the Content-Disposition header so that browsers
# present the option to save the file.
buffer.seek(0)
return FileResponse(buffer, as_attachment=True, filename='hello.pdf')
Uso la funcion drawString
de reportlab, pero solo me deja hacer esto p.drawString(x+dx/2, y+texty, todo)
con lo que puedo mostrar solo la clave y no la clave y el valor. Como puedo hacer para que se muestre la clave y el valor o si existe algún otra función de reportlab para poder hacerlo. Espero me puedan ayudar. Gracias de antemano
dic[key]
, no se nada de reportlab pero la clave es la variabletodo
ya accedes contodos[todo]
, pues eso es la sintaxis de los diccionarios.