Swiss QR facture - qrcodegen.py dans dossier différent du fichier Excel
Bonjour!
J'utilise le code version python utilisant le code Nayuki figurant sur https://forum.excel-pratique.com/excel/qr-factures-pour-la-suisse-105696/15#p945688:
' mike steelson
' https://forum.excel-pratique.com/excel/qr-factures-pour-la-suisse-105696
Option Explicit
Const pythonPath = """C:\Users\Michel\AppData\Local\Programs\Python\Python38-32\pythonw.exe"""
Sub ProduireQRCode()
Dim nav As Long, MyData As DataObject, i%, xq%, yq%, wq%, hq%, ff%
ChDir ThisWorkbook.Path
On Error Resume Next
' effacement de l'image et des fichiers le cas échéant
ActiveSheet.Shapes.Range(Array("myQRCode")).Delete
Kill "myQRCode.py"
Kill "myQRCode.svg"
On Error GoTo 0
' écriture fichier python
Close #1
ff = FreeFile
Open "myQRCode.py" For Output As #ff
With Sheets("py")
For i = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
Print #ff, .Cells(i, 1).Value
Next
End With
Close #ff
Shell (pythonPath & " myQRCode.py")
On Error GoTo messageErreur
' lieu de mise en place du QRCode
With ActiveSheet.Range("iciQRCode")
yq = .Top - 9: xq = .Left - 9
End With
wq = 146: hq = 146
' importation de la nouvelle image QRCode
With ActiveSheet.Pictures.Insert("myQRCode.svg")
.Width = wq: .Height = hq
.Left = xq: .Top = yq
.Name = "myQRCode"
End With
' mise en place de la croix
With ActiveSheet.Shapes.Range("croix")
.Left = xq: .Top = yq
.IncrementLeft (wq - .Width) / 2.12
.IncrementTop (hq - .Height) / 1.86
.ZOrder msoBringToFront
End With
Kill "myQRCode.py"
Kill "myQRCode.svg"
Exit Sub
messageErreur:
'indique le numéro et la description de l'erreur survenue
MsgBox "Erreur survenue ... " & "#" & vbCrLf & Err.Number & vbLf & Err.Description
Exit Sub
End Sub
Public Function Encode_UTF8(astr)
Dim c
Dim n
Dim utftext
utftext = ""
n = 1
Do While n <= Len(astr)
c = AscW(Mid(astr, n, 1))
If c < 128 Then
utftext = utftext + Chr(c)
ElseIf ((c >= 128) And (c < 2048)) Then
utftext = utftext + Chr(((c \ 64) Or 192))
utftext = utftext + Chr(((c And 63) Or 128))
ElseIf ((c >= 2048) And (c < 65536)) Then
utftext = utftext + Chr(((c \ 4096) Or 224))
utftext = utftext + Chr((((c \ 64) And 63) Or 128))
utftext = utftext + Chr(((c And 63) Or 128))
Else ' c >= 65536
utftext = utftext + Chr(((c \ 262144) Or 240))
utftext = utftext + Chr(((((c \ 4096) And 63)) Or 128))
utftext = utftext + Chr((((c \ 64) And 63) Or 128))
utftext = utftext + Chr(((c And 63) Or 128))
End If
n = n + 1
Loop
Encode_UTF8 = utftext
End Function
Sub dimensionnerCroix()
With ActiveSheet.Shapes.Range("croix")
.Width = 19
.LockAspectRatio = msoFalse
.Height = 19
Debug.Print .Width, .Height
End With
End Submodifié comme suit pour éviter la modification des dimensions du QRCode lors de l'impression excel :
Sub ProduireQRCode()
Dim nav As Long, MyData As DataObject, i%, xq%, yq%, wq%, hq%, ff%
ChDir ThisWorkbook.Path
On Error Resume Next
' effacement de l'image et des fichiers le cas échéant
ActiveSheet.Shapes.Range(Array("myQRCode")).Delete
Kill "myQRCode.py"
Kill "myQRCode.svg"
On Error GoTo 0
' écriture fichier python
Close #1
ff = FreeFile
Open "myQRCode.py" For Output As #ff
With Sheets("py")
For i = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
Print #ff, .Cells(i, 1).Value
Next
End With
Close #ff
Shell (pythonPath & " myQRCode.py")
On Error GoTo messageErreur
' lieu de mise en place du QRCode
With ActiveSheet.Range("iciQRCode")
yq = .Top - 9: xq = .Left - 9
End With
wq = 146: hq = 146
' importation de la nouvelle image QRCode
With ActiveSheet.Pictures.Insert("myQRCode.svg")
.Width = wq: .Height = hq
.Left = xq: .Top = yq
.Name = "myQRCode"
End With
' Déformation de la forme afin que le QR-Code soit imprimé carré
ActiveSheet.Shapes.Range(Array("myQRCode")).Select
Selection.ShapeRange.Width = 140.19
Selection.ShapeRange.LockAspectRatio = msoFalse ' Obligatoire, sinon le ratio 1/1 est rétabli par l'instruction ci-dessous.
Selection.ShapeRange.Height = 156.62
' mise en place de la croix
With ActiveSheet.Shapes.Range("croix")
.Left = xq: .Top = yq
.IncrementLeft (wq - .Width) / 2.12
.IncrementTop (hq - .Height) / 1.86
.ZOrder msoBringToFront
End With
Kill "myQRCode.py"
Kill "myQRCode.svg"
Exit Sub
messageErreur:
'indique le numéro et la description de l'erreur survenue
MsgBox "Erreur survenue ... " & "#" & vbCrLf & Err.Number & vbLf & Err.Description
Exit Sub
End Sub
Public Function Encode_UTF8(astr)
Dim c
Dim n
Dim utftext
utftext = ""
n = 1
Do While n <= Len(astr)
c = AscW(Mid(astr, n, 1))
If c < 128 Then
utftext = utftext + Chr(c)
ElseIf ((c >= 128) And (c < 2048)) Then
utftext = utftext + Chr(((c \ 64) Or 192))
utftext = utftext + Chr(((c And 63) Or 128))
ElseIf ((c >= 2048) And (c < 65536)) Then
utftext = utftext + Chr(((c \ 4096) Or 224))
utftext = utftext + Chr((((c \ 64) And 63) Or 128))
utftext = utftext + Chr(((c And 63) Or 128))
Else ' c >= 65536
utftext = utftext + Chr(((c \ 262144) Or 240))
utftext = utftext + Chr(((((c \ 4096) And 63)) Or 128))
utftext = utftext + Chr((((c \ 64) And 63) Or 128))
utftext = utftext + Chr(((c And 63) Or 128))
End If
n = n + 1
Loop
Encode_UTF8 = utftext
End Function
Sub dimensionnerCroix()
With ActiveSheet.Shapes.Range("croix")
.Width = 19
.LockAspectRatio = msoFalse
.Height = 19
Debug.Print .Width, .Height
End With
End SubCe code nécessite que le fichier qrcodegen.py soit enregistré dans le même dossier que le fichier excel. Je souhaiterais que ce fichier puisse par exemple être enregistré une fois sur un serveur, et que chaque fichier excel, chacun enregistré dans des dossiers différents, puisse aller le chercher à un seul endroit lors de la macro...
Une solution, étant précisé que je n'ai aucune compétence VBA?
Merci à vous!
As-tu essayé ceci :
- remplace
ChDir ThisWorkbook.Pathpar le dossier dans lequel tu mets qrcodegen.py
- et fais tourner la macro.
J'espère que les autres utilisateurs du programme pourront donner leur expérience.
Bonjour,
j'ai enfin pu tester la solution proposée ci-dessus
- créer un dossier
- y mettre qrcodegen.py
- indiquer dans la macro
ChDir "D://ledossier dans lequel se trouve qrcodegen.py"et cela semble fonctionner sans soucis