从 python 超链接内的模板创建多文件 docx
create multi file docx from a template within hyperlink in python
我正在尝试创建一个从 file.docx 开始的 python 程序,而 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 = 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()
这是 5 月原始 docx 的示例
[nome][cognome] email example@example.com
输出文件是这样的
output file 1-> john smith email
output file 2-> mario rossi email
再见,这是如何使用“python-docx”库
在 DOCX 文件中获取 link
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)
我正在尝试创建一个从 file.docx 开始的 python 程序,而 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 = 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()
这是 5 月原始 docx 的示例
[nome][cognome] email example@example.com
输出文件是这样的
output file 1-> john smith email
output file 2-> mario rossi email
再见,这是如何使用“python-docx”库
在 DOCX 文件中获取 link 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)