With this Python script I'm able to create a new pdf file called "my_file.pdf" and to add an acroForm editable text box:
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
pdf = canvas.Canvas("my_file.pdf", bottomup=0)
pdf.drawString(100, 100, "blablabla")
x = pdf.acroForm
x.textfield(value = "hello world!", fillColor = colors.yellow, borderColor = colors.black, textColor = colors.red, borderWidth = 2, borderStyle = 'solid', width = 500, height = 50, x = 50, y = 40, tooltip = None, name = None, fontSize = 20)
pdf.save()
When I open the "my_file.pdf" file with Adobe reader I see this:

But what I want, is to add the text box in an already existing pdf file called "input.pdf" (see next figure), instead of adding this box in a new pdf file "my_file.pdf".
To give you a hint, I'm already able to add a draw string (a not-editable text) to the existing pdf file called "input.pdf", and I obtain the edited file called "out.pdf" (see next figure):
from io import BytesIO
import pikepdf
from reportlab.pdfgen import canvas
import os
from reportlab.lib.units import cm
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from PyPDF2 import PdfFileReader, PdfFileWriter
from pypdf import PdfReader
text = "input.pdf"
def generate_stamp(msg, xy):
x, y = xy
buf = BytesIO() # This creates a BytesIO buffer for temporarily storing the generated PDF content.
c = canvas.Canvas(buf, bottomup=0) # This creates a canvas object using the BytesIO buffer. The bottomup=0 argument indicates that the coordinates increase from bottom to top (typical for PDFs).
c.setFontSize(16)
c.setFillColorCMYK(0, 0, 0, 0, alpha=0.7)
# c.rect(194, 5, 117, 17, stroke=1, fill=1)
c.setFillColorCMYK(0, 0, 0, 100, alpha=0.7)
c.drawString(x, y, msg)
c.save()
buf.seek(0)
return buf
stamp = generate_stamp('SOME TEXT STAMP', (300, 100))
# Add the comment to the first page of the pdf file
pdf_orig = pikepdf.open(text)
pdf_text = pikepdf.open(stamp)
formx_text = pdf_orig.copy_foreign(pdf_text.pages[0].as_form_xobject())
formx_page = pdf_orig.pages[0]
formx_name = formx_page.add_resource(formx_text, pikepdf.Name.XObject)
stamp_text = pdf_orig.make_stream(b'q 1 0 0 1 0 0 cm %s Do Q' % formx_name)
pdf_orig.pages[0].contents_add(stamp_text)
pdf_orig.save('./out.pdf')
I would like to have the same thing for the editable text box.


I solved this problem, I'm now able to add the editable text box inside an existing input.pdf file:
This is the complete Python code: