在python中从超链接中的模板创建多文件docx
我正在尝试创建一个python程序,它从file.docx开始,一个file.csv创建多个.docx文件。 问题是原始文件包含在创建新文件时丢失的超链接 这是我的python程序在python中从超链接中的模板创建多文件docx,python,python-docx,Python,Python Docx,我正在尝试创建一个python程序,它从file.docx开始,一个file.csv创建多个.docx文件。 问题是原始文件包含在创建新文件时丢失的超链接 这是我的python程序 from docx import Document from docx.shared import Inches import csv import os f = open('file.csv') nometofind = '[nome]' cognometofind = '[cognome]' reader
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()
这是may original docx的一个例子
[nome][cognome]电子邮件
输出文件如下所示
输出文件1->john smith电子邮件
输出文件2->mario rossi电子邮件
您好,这是如何在DOCX文件中获得带有“python DOCX”库的链接
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)