You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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?
The text was updated successfully, but these errors were encountered:
please clearly state what seems to be the problem: errors, expected vs. observed results etc.
it would help if you could produce a minimal example that shows your problem which does not reference other libraries like openpyxl and PyPDF
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.
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 = []
def fill_pdf_form(pdf_template, excel_file):
try:
# Leer los datos del archivo Excel
data = read_excel_data(excel_file)
fill_pdf_form('swap.pdf', 'datos.xlsx')
PLS HELP ME ,WHATS WRONG?
The text was updated successfully, but these errors were encountered: