1 |
|
2 |
|
3 |
|
4 | import sys
|
5 | import subprocess
|
6 | import os
|
7 | import csv
|
8 | from shutil import copyfile
|
9 |
|
10 |
|
11 | def GetElement(file, i, j):
|
12 | with open(file, newline='') as csvfile:
|
13 | reader = csv.reader(csvfile, delimiter=',', quotechar='|')
|
14 | for line in reader:
|
15 | if reader.line_num - 1 == i:
|
16 | return line[j]
|
17 |
|
18 |
|
19 |
|
20 | def NombreEtu(filename):
|
21 | with open(filename, 'r') as f:
|
22 | i = -1
|
23 | for ligne in f:
|
24 | i += 1
|
25 | return i
|
26 |
|
27 | if sys.argv[1] == "?" :
|
28 | print("[DossierInitial] [DossierRenommer] [DossierCrypter] [OwnerPwd] [Generic] [Denomination] [FichierCSV]")
|
29 | print("Generic : Nom générique des fichiers à renommer. Par exemple : MAT_PDFSAM_ pour MAT_PDFSAM_1.pdf, MAT_PDFSAM_2.pdf, etc.")
|
30 | print("Denomination, par exemple : DS2_MAT2")
|
31 |
|
32 | sys.exit()
|
33 |
|
34 |
|
35 | NomDossierInitial = sys.argv[1]
|
36 | NomDossierRenommer = sys.argv[2]
|
37 | NomDossierCrypter = sys.argv[3]
|
38 | OwnerPwd = sys.argv[4]
|
39 | Generic = sys.argv[5]
|
40 | Denomination = sys.argv[6]
|
41 | FichierCSV = sys.argv[7]
|
42 |
|
43 |
|
44 | k = 0
|
45 | NombreEtudiants = NombreEtu(FichierCSV)
|
46 |
|
47 | thepath = os.getcwd()
|
48 | repini = thepath+"/"+NomDossierInitial
|
49 | reprenom = thepath+"/"+NomDossierRenommer
|
50 | repcrypt = thepath+"/"+NomDossierCrypter
|
51 |
|
52 |
|
53 | with open('Reperage_Numeros_Sujets.csv', 'w', newline='',encoding='utf-8') as csvfile:
|
54 | spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
|
55 | spamwriter.writerow(['Num-Sujet','Nom','Prenom','QCM-Number'])
|
56 |
|
57 |
|
58 |
|
59 | for j in range(1,NombreEtudiants + 1) :
|
60 | File = os.path.join(repini, Generic+str(j)+".pdf")
|
61 |
|
62 | k = k + 1
|
63 |
|
64 | if k > NombreEtudiants :
|
65 | break
|
66 |
|
67 | NomETU = GetElement(FichierCSV,k,0)
|
68 | PrenomETU = GetElement(FichierCSV,k,1)
|
69 | ReadPwd = GetElement(FichierCSV,k,2)
|
70 | NomFichierRenommer = reprenom+"/"+NomETU+"_"+PrenomETU+"_"+Denomination+"_Sujet-Numero_"+str(k)+".pdf"
|
71 | NomFichierCrypter = repcrypt+"/"+NomETU+"_"+PrenomETU+"_"+Denomination+"_Sujet-Numero_"+str(k)+"_p.pdf"
|
72 |
|
73 | copyfile(File, NomFichierRenommer)
|
74 |
|
75 | subprocess.call(["qpdf","--encrypt",ReadPwd,OwnerPwd,"256","--",File,NomFichierCrypter])
|
76 |
|
77 | with open('Reperage_Numeros_Sujets.csv', 'a', newline='',encoding='utf-8') as csvfile:
|
78 | spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
|
79 | spamwriter.writerow([str(k),NomETU,PrenomETU,ReadPwd])
|
80 |
|
81 |
|