How to generate ID card or certificate in Django with any kind of template as MS Word or PPT or PDF?

79 views Asked by At

I need help to make a ID card or document in django to provide user their ID card or any dynamic documents such as certificates. And provide as PDF

I tried, Python docx library. with that it is possible to create a docx file and even add an image. but if i use a template and when i want to replace a text in the docx and the text is in text-box in word then its not replacing. Btw, I solved that. but now i can't replate a image in appropriate place in word. SEE THE TEMPLATE IMAGE . Here if i want to add an image and the image i want to place is in the center Top. but i can't do it.

So Please if you can help with this that's fine else , Please give a way that how can i edit a document template with python Just need to generate dynamic document.

CODE :

def myreplace(file, regex, replace):
    for p in file.paragraphs:
        if regex.search(p.text):
            inline=p.runs
            for i in range(len(inline)):
                if regex.search(inline[i].text):
                    text=regex.sub(replace, inline[i].text)
                    inline[i].text=text
    for table in file.tables:
        for row in table.rows:
            for cell in row.cells:
                myreplace(cell, regex, replace)


def ReadingTextDocuments(fileName): 

    doc = Document (fileName)
    completedText = []
    for paragraph in doc.paragraphs: 
        completedText.append (paragraph.text)
    return '\n'.join(completedText)





file_path = os.path.join(settings.BASE_DIR, 'static', 'doc', 'newx.docx')
placeholders = {
    'USER_NAME': str(request.user.student.s_name),
    'ID_FOR_USER': str(request.user.student.s_id),
    'CLS_FOR_USER': str(request.user.student.class_subjects),
    'BATCH_FOR_USR': str(request.user.student.batch.batch_name),
}

document = Document(file_path)
adsds= request.user.student
image_path =  os.path.join(settings.MEDIA_ROOT, f'{adsds.profile_pic}' )

document.add_picture(image_path, width=Inches(1), height=Inches(1))

for placeholder, replacement in placeholders.items():
    target = re.compile(placeholder)
    myreplace(document, target, replacement)

modified_file_path = os.path.join(settings.BASE_DIR, 'static', 'doc', f'new{random.randint(1, 10)}.docx')
document.save(modified_file_path)


document = Document(file_path)
adsds= request.user.student
image_path =  os.path.join(settings.MEDIA_ROOT, f'{adsds.profile_pic}' )

document.add_picture(image_path, width=Inches(1), height=Inches(1))

for placeholder, replacement in placeholders.items():
    target = re.compile(placeholder)
    myreplace(document, target, replacement)

modified_file_path = os.path.join(settings.BASE_DIR, 'static', 'doc', f'new{random.randint(1, 10)}.docx')

adsds= request.user.student
image_path =  os.path.join(settings.MEDIA_ROOT, f'{adsds.profile_pic}' )
document.add_picture(image_path, width=Inches(0.5), height=Inches(0.5))

document.save(modified_file_path)
0

There are 0 answers