1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| from PyQt4 import QtCore
import zipfile, os.path, glob, os, shutil, sys
PROJECT_PATH = os.path.dirname(sys.argv[0])
lib = ['$$nom$$', '$$prenom$$', '$$age$$']
liste = ['Mon nom', 'mon prénom', 'mon age']
class ModifOpenXml():
def __init__(self, src, dst, listeIn, listeOut):
self.src = PROJECT_PATH +"\\"+ src
self.temp = PROJECT_PATH +"\\temp"
self.tempZip = PROJECT_PATH +"\\temp.zip"
self.dst = dst
self.listeIn = listeIn
self.listeOut = listeOut
def modifOpenXml(self):
shutil.copyfile(self.src, self.tempZip)
zfile = zipfile.ZipFile(self.tempZip, 'r')
zfile.extractall(self.temp)
zfile.close()
formatOOo = (self.dst[len(self.dst)-4:len(self.dst)])
formatMS = (self.dst[len(self.dst)-5:len(self.dst)])
if formatOOo == ".ods" or formatOOo== ".odt":
content = self.temp+'\\content.xml'
elif formatMS== ".xlsx":
content = self.temp+'\\xl\\sharedStrings.xml'
file = QtCore.QFile(content)
file.open(QtCore.QIODevice.ReadOnly)
ch = file.readAll()
for i in self.listeIn:
ch.replace(i, self.listeOut[self.listeIn.index(i)].encode('utf-8'))
file.close()
os.remove(content)
file = QtCore.QFile(content)
file.open(QtCore.QIODevice.WriteOnly)
file.write(ch)
file.close()
lenpathparent = len(self.temp)+1
def zipdirectory(zfile, path):
for i in glob.glob(path+'\\*'):
if os.path.isdir(i): zipdirectory(zfile, i )
else: zfile.write(i, i[lenpathparent:])
zfile = zipfile.ZipFile(self.dst,'w',compression=zipfile.ZIP_DEFLATED)
zipdirectory(zfile, self.temp)
zfile.close()
shutil.rmtree(self.temp)
os.remove(self.tempZip)
def select(num):
if num == 1:
app = ModifOpenXml('template.ods', 'fichier_de_sortie.ods', lib, liste)
elif num == 2:
app = ModifOpenXml('template.odt', 'fichier_de_sortie.odt', lib, liste)
elif num == 3:
app = ModifOpenXml('template.xlsx', 'fichier_de_sortie.xlsx', lib, liste)
app.modifOpenXml()
select(2) |
Partager