-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReportDate-with-CSV-to-PDF.py
More file actions
34 lines (26 loc) · 1.04 KB
/
ReportDate-with-CSV-to-PDF.py
File metadata and controls
34 lines (26 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import pandas as pd
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
def leer_csv(nombre_archivo):
# Leer el archivo CSV y devolver un DataFrame de pandas
return pd.read_csv(nombre_archivo)
def generar_pdf(datos):
# Configurar el tamaño del PDF
c = canvas.Canvas("datos.pdf", pagesize=letter)
# Configurar el tamaño de la fuente y el espacio entre líneas
c.setFont("Helvetica", 12)
espacio_linea = 15
# Iterar sobre los datos y escribirlos en el PDF
for fila, (_, datos_fila) in enumerate(datos.iterrows()):
# Escribir cada campo de la fila en una nueva línea
for columna, valor in datos_fila.iteritems():
texto = f"{columna}: {valor}"
c.drawString(100, 800 - (espacio_linea * (fila + 1)), texto)
# Guardar el PDF generado
c.save()
if __name__ == "__main__":
nombre_archivo = "datos.csv"
# Leer datos del archivo CSV
datos = leer_csv(nombre_archivo)
# Generar PDF con los datos
generar_pdf(datos)