Search code examples
pythonpython-docx

create multi file docx from a template within hyperlink in python


I'm trying to create a python program that starts from a file.docx and a file.csv creates multiple .docx files. The problem is that the original file contains hyperlinks that are lost in the creation of new files

this is my python program

from docx import Document
from docx.shared import Inches

import csv
import os


f = open('file.csv')
nometofind = '[nome]'
cognometofind = '[cognome]'
reader = csv.reader(f)
next(reader)
for row in reader:

    cognome=row[0]
    nome=row[1]

    nometoreplace = nome
    cognometoreplace = cognome

    document = Document('OriginalFile.docx')
    
    for par in document.paragraphs:
        par.text = par.text.replace(nometofind, nometoreplace)
        par.text = par.text.replace(cognometofind, cognometoreplace)

    document.save(nome+cognome+'.docx')

f.close()

and this is an example of may original docx

[nome][cognome] email [email protected]

the output files are like these

output file 1-> john smith email

output file 2-> mario rossi email


Solution

  • Ciao, this is how to get a link inside DOCX file with "python-docx" library

        from docx import Document
        from docx.opc.constants import RELATIONSHIP_TYPE as RT
    
        document = Document('nameFile.docx')
        rels = document.part.rels
    
        def leggiLink(rels):
            for rel in rels:
                if rels[rel].reltype == RT.HYPERLINK:
                    yield rels[rel]._target
        print('print link inside file: nameFile.docx')
    
        for i in leggiLink(rels):
            print(i)