Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill PDF from Excel sheet #245

Open
pablo1strange opened this issue May 7, 2024 · 1 comment
Open

Fill PDF from Excel sheet #245

pablo1strange opened this issue May 7, 2024 · 1 comment

Comments

@pablo1strange
Copy link

import openpyxl
import pdfrw
from PyPDF2.generic import TextStringObject, NameObject

def read_excel_data(excel_file):
try:
workbook = openpyxl.load_workbook(excel_file)
sheet = workbook.active
data = []

    # Leer los datos del archivo Excel
    for row in sheet.iter_rows(min_row=2, values_only=True):
        row_data = {}
        for idx, value in enumerate(row, start=1):
            header = sheet.cell(row=1, column=idx).value
            row_data[header] = value
        data.append(row_data)
    
    return data
except Exception as e:
    print("Error al leer los datos del archivo Excel:", e)
    return []

def fill_pdf_form(pdf_template, excel_file):
try:
# Leer los datos del archivo Excel
data = read_excel_data(excel_file)

    # Abrir la plantilla del PDF
    template_pdf = pdfrw.PdfReader(pdf_template)

    # Iterar sobre las páginas del PDF
    for page in template_pdf.pages:
        annotations = page.get('/Annots')
        if annotations:
            for annotation in annotations:
                if annotation.get('/Subtype') == '/Widget':
                    field_name = annotation.get('/T')
                    if field_name:
                        for row_data in data:
                            if field_name in row_data:
                                annotation.update({
                                    NameObject('/V'): TextStringObject(str(row_data[field_name]))
                                })
                                break

    # Escribir el PDF rellenado en un archivo de salida
    output_pdf = 'pdfrellenado.pdf'
    writer = pdfrw.PdfWriter()
    writer.write(output_pdf, template_pdf)

    print("Formulario rellenado con éxito.")
except Exception as e:
    print("Se ha producido un error:", e)

fill_pdf_form('swap.pdf', 'datos.xlsx')

PLS HELP ME ,WHATS WRONG?

@sl2c
Copy link

sl2c commented May 13, 2024

Hi!

  1. please clearly state what seems to be the problem: errors, expected vs. observed results etc.
  2. it would help if you could produce a minimal example that shows your problem which does not reference other libraries like openpyxl and PyPDF
  3. I'm not sure you can easily combine PyPDF's TextStringObject with pdfrw. I could be wrong, however if you indeed can then this would probably not be very robust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants