Bonjour,
Retenu par des obligations familiales, je n' ai pus poursuivre cette discussion. Suivant votre demande, voici tout d' abord le script.:
Code : Tout sélectionner
#!/usr/bin/env python3
from uno import *
from exiftool import *
from fractions import Fraction
from datetime import datetime
from sys import *
from os import chdir
import csv
def fntslc(event=None):
ctx = XSCRIPTCONTEXT.getComponentContext()
create = ctx.ServiceManager.createInstance
dsr = create("com.sun.star.ui.dialogs.OfficeFilePicker")
dsr.setDisplayDirectory("file:///media/elfouste/El_Casot/Yvon/Phototeque1/A_Trier/")
dsr.execute()
global fchslc
fchslc = dsr.Files[0]
# Ouverture de la fenêtre de sélection
fntslc()
# Clé du fichier
cfpht = 1
# Chemin complet du fichier
global chmcmppht
bls = '/'
pstbls = fchslc.rfind(bls,0,10)
chmcmppht = fchslc[pstbls:]
# Récupération des données exif
def dnxf():
global chmpht; global nmpht; global dtpht; global hrpht; global tpbjc
global dmspht; global tmpxps; global fcl; global lngfcl; global lmn; global pstfls
# Chemin du fichier
xbls = chmcmppht.count(bls)
i = 0
while i <= xbls:
pstbls1 = chmcmppht.rfind(bls)
i = i + 1
chmpht = chmcmppht[pstbls-1:pstbls1]
# Nom de la photo
bls2 = '.'
pstbls2 = chmcmppht.rfind(bls2)
nmpht = chmcmppht[pstbls1 + 1:pstbls2]
# Extraction de la date et de l' heure de prise de vue
dt = datetime.strptime(dtcrt,"%Y:%m:%d %H:%M:%S")
dtpht = dt.strftime('%d-%m-%Y')
hrpht = dt.strftime('%H:%M:%S')
# Type d' objectif utilisé
if bjc == 53:
tpbjc = 'Canon EF-S 18-55mm f/3.5-5.6 III'
elif bjc == 39:
tpbjc = 'Canon EF 75-300mm f/4-5.6'
else:
tpbjc = 'Inconnu'
# Dimension de la photo
dmspht = '%s*%s' % (lrg, htr)
# Temps d' exposition
x = Fraction(xps).limit_denominator()
nmr = x.numerator
dnm = x.denominator
tmpxps = '%s/%s' % (nmr, dnm)
# Valeur de focale
fcl = 'f '+ str(round(vlrfcl, 1))
lngfcl = str(vlrlngfcl)+ ' mm'
# Valeur de luminosité
lmn = round(vlrlmn,1)
# Valeur de flash
if pstflash == 16:
pstfls = 'Inactif'
else:
pstfls = 'Actif'
#Extraction du fabricant de l' appareil
with ExifTool() as e:
marque = e.get_tag('Make', chmcmppht)
if marque == None:
print ("Il n' y a pas de données exif pour cette photographie !")
marque = 'Inconnue'; mdl = 'Inconnue'; dtpht = 'Inconnue'; hrpht = 'Inconnue'
dmspht = 'Inconnue'; tmpxps = 'Inconnue'; fcl = 'Inconnue'; lngfcl = 'Inconnue'
iso = 'Inconnue'; vrtbjc = 'Inconnue'; lmn = 'Inconnue'; flash = 'Inconnue'
bjc = 'Inconnue'
elif marque == 'Canon':
with ExifTool() as e:
xtnpht = e.get_tag('FileTypeExtension', chmcmppht) # Extention du fichier
mdl = e.get_tag('Model', chmcmppht) # Modèle appareil
dtcrt = e.get_tag('DateTimeOriginal', chmcmppht) # Date et heure prise de vue
bjc = e.get_tag('LensType', chmcmppht) # Objectif utilisé
lrg = e.get_tag('ImageWidth', chmcmppht)
htr = e.get_tag('ImageHeight', chmcmppht) # Dimension de la photo
#snspht = e.get_tag('Orientation', chmcmppht) # Orientation de la photo
xps = e.get_tag('ExposureTime', chmcmppht) # Temps d' exposition
vlrfcl = e.get_tag('FNumber', chmcmppht) # Focale
vlrlngfcl = e.get_tag('FocalLength', chmcmppht) # Longueur de focale
iso = e.get_tag('Iso', chmcmppht) # Sensibilité
vrt = e.get_tag('ApertureValue', chmcmppht) # Ouverture objectif
vlrlmn = e.get_tag('LightValue', chmcmppht) # Luminosité
pstflash = e.get_tag('Flash', chmcmppht) # Position flash
dnxf()
elif marque == 'FUJIFILM':
with ExifTool() as e:
xtnpht = e.get_tag('FileTypeExtension', chmcmppht) # Extention du fichier
mdl = e.get_tag('Model', chmcmppht) # Modèle appareil
dtcrt = e.get_tag('DateTimeOriginal', chmcmppht) # Date et heure prise de vue
bjc = e.get_tag('LensType', chmcmppht) # Objectif utilisé
lrg = e.get_tag('ImageWidth', chmcmppht) # Objectif utilisé
htr = e.get_tag('ImageHeight', chmcmppht)
#snspht = e.get_tag('Orientation', chmcmppht) # Orientation de la photo
xps = e.get_tag('ExposureTime', chmcmppht) # Temps d' exposition
vlrfcl = e.get_tag('FNumber', chmcmppht) # Focale
vlrlngfcl = e.get_tag('FocalLength', chmcmppht) # Longueur de focale
iso = e.get_tag('Iso', chmcmppht) # Sensibilité
vrt = e.get_tag('ApertureValue', chmcmppht) # Ouverture objectif
vlrlmn = e.get_tag('LightValue', chmcmppht) # Luminosité
pstflash = e.get_tag('Flash', chmcmppht) # Position flash
dnxf()
else:
with ExifTool() as e:
xtnpht = e.get_tag('FileTypeExtension', chmcmppht) # Extention du fichier
mdl = e.get_tag('Model', chmcmppht) # Modèle appareil
dtcrt = e.get_tag('DateTimeOriginal', chmcmppht) # Date et heure prise de vue
bjc = e.get_tag('LensType', chmcmppht) # Objectif utilisé
lrg = e.get_tag('ImageWidth', chmcmppht) # Objectif utilisé
htr = e.get_tag('ImageHeight', chmcmppht) # Dimension de la photo
#snspht = e.get_tag('Orientation', chmcmppht) # Orientation de la photo
xps = e.get_tag('ExposureTime', chmcmppht) # Temps d' exposition
vlrfcl = e.get_tag('FNumber', chmcmppht) # Focale
vlrlngfcl = e.get_tag('FocalLength', chmcmppht) # Longueur de focale
iso = e.get_tag('Iso', chmcmppht) # Sensibilité
vrt = e.get_tag('ApertureValue', chmcmppht) # Ouverture objectif
vlrlmn = e.get_tag('LightValue', chmcmppht) # Luminosité
pstflash = e.get_tag('Flash', chmcmppht) # Position flash
dnxf()
#Sauvegarde des données exif
chdir('/home/elfouste/Documents/Python/Svg_Csv')
with open('DnExif.csv', 'w') as f:
f.write('Cf_Pht'); f.write(','); f.write('ChmPht'); f.write(','); f.write('NmPht');
f.write(','); f.write('XtnPht'); f.write(','); f.write('Fbc'); f.write(','); f.write('MdlApp');
f.write(','); f.write('Bjc'); f.write(','); f.write('DtPht'); f.write(','); f.write('HrPht');
f.write(','); f.write('DmsPht'); f.write(','); f.write('TmpXps'); f.write(','); f.write('Fcl');
f.write(','); f.write('LngFcl'); f.write(','); f.write('Sns'); f.write(','); f.write('Lmn');
f.write(','); f.write('PstFlsh'); f.write(','); f.write('ChmCmpPht')
f.write('\n')
f.write(str(cfpht)); f.write(','); f.write(chmpht); f.write(','); f.write(nmpht); f.write(',');
f.write(xtnpht); f.write(','); f.write(marque); f.write(','); f.write(mdl); f.write(',');
f.write(tpbjc); f.write(','); f.write(str(dtpht)); f.write(','); f.write(str(hrpht)); f.write(',');
f.write(str(dmspht)); f.write(','); f.write(str(tmpxps)); f.write(','); f.write(fcl); f.write(',');
f.write(lngfcl); f.write(','); f.write(str(iso)); f.write(','); f.write(str(lmn)); f.write(',');
f.write(pstfls); f.write(','); f.write(chmcmppht)
Ce script se lance depuis le formulaire
F_Pht par le bouton
Sélectionner une photo.. La commande est placé dans les éventements à la ligne
exécuter une action
Remarque:
Il faut que je le peaufine car, s'il fonctionne bien la première fois, après, je suis obligé de le réinitialiser.
Pour ce qui est de la macro, en voici le code:
Code : Tout sélectionner
Dim maConnexion as Object, oConnexion as Object, oForm As Object
Sub AjoutDonnees
Dim DrvMan As Object, maRequete as Object, Coding as Object
Dim cheminCSV As String, URLbdcsv As String, instrSQL as String
Dim Infos(3) As New com.sun.star.beans.PropertyValue
Dim x As Long '***
Dim sNomFichier As String '***
Dim lsNomFichier As Integer '***
ThisDatabaseDocument.CurrentController.connect("","")
maConnexion = ThisDatabasedocument.CurrentController.ActiveConnection
DrvMan = CreateUnoService("com.sun.star.sdbc.DriverManager")
Coding = CreateUnoService("com.sun.star.sdbc.FLATConnectionProperties")
cheminCSV = "/home/elfouste/Documents/Python/Svg_Csv/DnExif.csv"
' extraction du nom de fichier sélectionné '***
sNomFichier = cheminCSV
lsNomFichier = len(sNomFichier)
x = lsNomFichier
while mid(sNomFichier,x,1) <> "/"
x = x-1
wend
sNomFichier = mid(sNomFichier,x + 1,lsNomFichier-x-4)
URLbdcsv = "sdbc:flat:" & cheminCSV
Infos(0).Name = "HeaderLine"
Infos(0).Value = True
Infos(1).Name = "FieldDelimiter"
Infos(1).Value = chr(44)
Infos(2).Name = "StringDelimiter"
Infos(2).Value = ","
Infos(3).Name = "Extension"
Infos(3).Value = "csv"
oConnexion = DrvMan.getConnectionWithInfo(URLbdcsv, Infos())
maRequete = maConnexion.createStatement()
oForm = thisComponent
instrSQL = "DELETE FROM ""Tp_Pht"""
maRequete.executeUpdate(instrSQL)
CopierDonnees(sNomFichier)
End Sub
'========================================================================================================
Sub CopierDonnees(NomFichier)
On Error GoTo CopierDonnees_Err
Dim unRowSet as Object, maRequete as Object, resuQuery as Object, maRequete2 as object, Resultat as Object
Dim Fenetre as Object, FenetreForm as Object, avance as Object
Dim instrSQL as String, instrSQL2 as String, i as Integer, dteNaiss as Date, Compte as Integer, x as Integer
Fenetre = ThisDatabaseDocument.CurrentController.Frame.ContainerWindow
FenetreForm = oForm.currentcontroller.Frame.ContainerWindow
Fenetre.Enable = False
FenetreForm.Enable = False
avance = oForm.CurrentController.StatusIndicator
unRowSet = createUnoService("com.sun.star.sdb.RowSet")
instrSQL = "SELECT * FROM " & NomFichier
instrSQL2 = "SELECT COUNT(*) as ""nb"" FROM " & NomFichier
maRequete = oConnexion.createStatement()
maRequete2 = oConnexion.createStatement()
Resultat = maRequete2.executeQuery(instrSQL2)
Resultat.Next
Compte = resultat.getInt(1)
resuQuery = maRequete.executeQuery(instrSQL)
With unRowSet
.ActiveConnection = maConnexion
.CommandType = com.sun.star.sdb.CommandType.TABLE
.Command = "Tp_Pht"
.Execute
x = 1
avance.start("Veuillez patienter ...", Compte)
Do While resuQuery.Next
.moveToInsertRow
For i = 1 to 17
Select Case .Columns.getByIndex(i -1).TypeName
Case "INTEGER"
.Columns.getByIndex(i -1).updateInt(resuQuery.getInt(i))
Case "VARCHAR"
dim z as string
if i = 9 then
z = left(resuQuery.getString(9),8)
.Columns.getByIndex(i -1).updateString(z)
else
.Columns.getByIndex(i -1).updateString(resuQuery.getString(i))
end if
Case "NUMERIC"
.Columns.getByIndex(i -1).updateInt(resuQuery.getInt(i))
End Select
If i = 1 Then
.insertRow
Else
.UpdateRow
End If
Next i
avance.Value = x
avance.Text = "Ligne " & x & " recopiée"
x = x + 1
Loop
avance.Text = "Terminé " & Compte & " lignes recopiées"
End With
oConnexion.Dispose
unRowSet.Dispose
Wait 800
avance.End
FenetreForm.Enable = True
Fenetre.Enable = True
' Enregistrement des données exif
SvgDnXf
Frm.Next
Frm.Previous
CopierDonnees_Exit:
On Error GoTo 0
Exit Sub
CopierDonnees_Err:
MsgBox(Error, 16)
FenetreForm.Enable = True
Fenetre.Enable = True
oConnexion.Dispose
unRowSet.Dispose
Resume CopierDonnees_Exit
End Sub
Function SvgDnXf()
Dim DBContext as object, DB as object,DBase as Object, Statement as object, Rqt As Object
Dim RqtSql as string, Fch as string
Fch = ConvertToURL("file:///media/elfouste/El_Casot/Yvon/Phototeque/Phototeque_HSQL.odb")
DBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
DB = DBContext.getbyName(Fch)
DBase = DB.getConnection("","")
Statement = DBase.createStatement()
RqtSql = "INSERT INTO ""T_Pht""(""ChmPht"",""NmPht"",""XtnPht"",""Fbc"","&_
"""Mdl"",""TpBjc"",""DtPht"",""HrPht"",""DmsPht"",""TmpXps"","&_
"""Fcl"",""LngFcl"",""Iso"",""Lmn"",""PstFls"",""ChmCmpPht"")"&_
" SELECT ""ChmPht"",""NmPht"",""XtnPht"",""Fbc"",""MdlApp"",""Bjc"","&_
"""DtPht"",""HrPht"",""DmsPht"",""TmpXps"",""Fcl"",""LngFcl"",""Iso"","&_
"""Lmn"",""PstFls"",""ChmCmp"" FROM ""Tp_Pht"""
Rqt = Statement.executeQuery(RqtSqL)
MsgBox "Les données exif ont été enregistrées dans la table T_Pht"
End Function
Le bouton
Importer une photo permet de lancer la macro. Au même titre que le précédent bouton, l' appel ce fait depuis exécuter l' action.
Par sécurité, je passe par une table provisoire
Tp_Pht et si le déroulement se passe bien, les données sont sauvegarder dans la table définitive par la fonction
SvgDnExif.
Cordialement