[PYTHON] Comment convertir des pages Web en PDF, PNG, JPG avec VBA (Excel) (Selenium Basic)

Comment convertir des pages Web en PDF, PNG, JPG avec VBA (Excel)

-Aperçu- Je l'ai fait parce que je l'ai souvent utilisé dans les affaires.

Selenium est un outil qui automatise facilement les opérations du navigateur. J'entends souvent comment utiliser Selenium avec Python ou JavaScript, mais en fait, il peut être exécuté avec Visual Basic.

Surtout au Japon, je pense que de nombreuses entreprises sont fortement dépendantes d'Excel, donc utiliser Excel comme interface graphique ne semble pas avoir de résistance psychologique. (Je pense que j'ai beaucoup à dire ...)

-procédure-

  1. Téléchargez Selenium Basic depuis Selenium Release.
  2. Installez le fichier ci-dessus.
  3. Téléchargez le pilote du navigateur dont vous avez besoin. (Cette fois, j'utiliserai Chrome) Chrome Driver
  4. Mettez à jour chromedriver.exe dans C: \ Users \ [nom d'utilisateur] \ AppData \ Local \ Selenium Basic avec chromedriver.exe téléchargé dans 3. (Juste écraser le fichier)
  5. Écrivez vba

-Détails- 1.2. L'URL etc. peut changer. Veuillez vérifier chacun d'eux. Vous pouvez l'installer par défaut. 3. Là encore, l'URL peut changer. Veuillez vérifier chacun d'eux. Pour Chrome, veuillez télécharger la même version de Chrome que vous utilisez. 4. Remplacez simplement. 5.

Si vous passez à 4, la bibliothèque de types de sélénium apparaîtra dans Outils-> Paramètres de référence dans l'environnement de développement Excel, alors vérifiez-le. Si vous ne le faites pas, vous ne pourrez pas utiliser Selenium. Vérifiez également les paramètres de référence tels que Microsoft Scripting Runtime.

Voici un exemple. à ~ (enregistrer le répertoire de destination, enregistrer le nom du fichier, l'URL de la page Web, le numéro de ligne de la feuille (pour la confirmation ou l'écriture du répertoire de sauvegarde), la feuille à gérer si elle est enregistrée ou non)

Pour PDF

Option Explicit

Sub toPDF(ByVal directory, ByVal filename, ByVal url, ByVal i, ByVal sheetn As String)
  On Error GoTo myerror:
    Dim sheet1 As Worksheet
    Set sheet1 = Sheets(sheetn)
    Dim sPath As String, WSH As Variant
    Set WSH = CreateObject("WScript.Shell")
    sPath = WSH.SpecialFolders("Desktop") & "\"
    If (directory = "") Then directory = sPath
    If (Right(directory, 1) <> "\") Then directory = directory & "\"
    If (filename = "") Then filename = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
    Dim savepath As String
    savepath = directory & filename & ".pdf"
    Dim driver As New Selenium.ChromeDriver
    driver.SetPreference "download.default_directory", directory
    driver.SetPreference "download.directory_upgrade", True
    driver.SetPreference "download.prompt_for_download", False
    driver.SetPreference "safebrowsing.enabled", True
    driver.SetPreference "plugins.plugins_disabled", Array("Chrome PDF Viewer")
    driver.AddArgument "headless"
    driver.AddArgument "disable-gpu"
    driver.AddArgument "hide-scrollbars"
    Dim w As Long
    Dim h As Long

    driver.Start
    driver.Get url

    w = driver.ExecuteScript("return document.body.scrollWidth")
    h = driver.ExecuteScript("return document.body.scrollHeight")
    
    Dim pdf As Object
    
    driver.Window.SetSize w, h
    
    Set pdf = CreateObject("Selenium.PdfFile")
    pdf.SetPageSize 210, 297, "mm"
    pdf.AddImage driver.TakeScreenshot, True
    pdf.SaveAs savepath
    sheet1.Cells(i, 5).Value = 1
    sheet1.Cells(i, 6).Value = savepath
    driver.Quit
    Exit Sub
myerror:
    MsgBox "no"
    sheet1.Cells(i, 5).Value = 0
End Sub
Sub dopdf()
  Dim sPath As String, WSH As Variant
    Set WSH = CreateObject("WScript.Shell")
    sPath = WSH.SpecialFolders("Desktop") & "\"
    Dim directory As String
    Dim filename As String
    directory = sPath
    If (Right(Len(directory), 1) <> "\") Then directory = directory & "\"
    
  Dim sheet1 As String
  Dim i As Long
  
  sheet1 = "Pour pdf"
  
  Dim sheetn As Worksheet
  Set sheetn = Sheets(sheet1)
  Dim r As Long
  r = sheetn.Cells(Rows.Count, 4).End(xlUp).Row
  For i = 2 To r
    If (sheetn.Cells(i, 4).Value = "") Then
      GoTo a1:
    End If
    filename = sheetn.Cells(i, 3).Text
    If (filename = "") Then filename = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
    Dim result
    result = Dir(sheetn.Cells(i, 2).Text, vbDirectory)
    If (directory <> "" Or result <> True) Then sPath = sheetn.Cells(i, 2).Text
    Call toPDF(sPath, filename, sheetn.Cells(i, 4).Text, i, sheet1)
a1:
  Next i
End Sub

Pour JPG

Option Explicit

Sub toJPG(ByVal directory, ByVal filename, ByVal url, ByVal i, ByVal sheetn As String)
  On Error GoTo myerror:
    Dim sheet1 As Worksheet
    Set sheet1 = Sheets(sheetn)
    Dim sPath As String, WSH As Variant
    Set WSH = CreateObject("WScript.Shell")
    sPath = WSH.SpecialFolders("Desktop") & "\"
    If (directory = "") Then directory = sPath
    If (Right(directory, 1) <> "\") Then directory = directory & "\"
    If (filename = "") Then filename = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
    Dim savepath As String
    savepath = directory & filename & ".jpg "
    Dim driver As New Selenium.ChromeDriver
    driver.SetPreference "download.default_directory", directory
    driver.SetPreference "download.directory_upgrade", True
    driver.SetPreference "download.prompt_for_download", False
    driver.SetPreference "safebrowsing.enabled", True
    driver.SetPreference "plugins.plugins_disabled", Array("Chrome PDF Viewer")
    driver.AddArgument "headless"
    driver.AddArgument "disable-gpu"
    driver.AddArgument "hide-scrollbars"
    Dim w As Long
    Dim h As Long
    
    driver.Start
    driver.Get url
    driver.FindElementByClass("tab02").Click
    driver.ExecuteScript ("this.document.getElementById('tab01').setAttribute('class','tabContent01');")
    driver.ExecuteScript ("this.document.getElementById('tab03').setAttribute('class','tabContent03');")
    w = driver.ExecuteScript("return document.body.scrollWidth")
    h = driver.ExecuteScript("return document.body.scrollHeight")
    driver.Window.SetSize w, h
    driver.TakeScreenshot.SaveAs savepath
    sheet1.Cells(i, 5).Value = 1
    sheet1.Cells(i, 6).Value = savepath
    driver.Quit
    Exit Sub
myerror:
    
    sheet1.Cells(i, 5).Value = 0
End Sub
Sub dojpg()
  Dim sPath As String, WSH As Variant
    Set WSH = CreateObject("WScript.Shell")
    sPath = WSH.SpecialFolders("Desktop") & "\"
    Dim directory As String
    Dim filename As String
    directory = sPath
    If (Right(Len(directory), 1) <> "\") Then directory = directory & "\"
    
  Dim sheet1 As String
  Dim i As Long
  
  sheet1 = "Pour jpg"
  
  Dim sheetn As Worksheet
  Set sheetn = Sheets(sheet1)
  Dim r As Long
  r = sheetn.Cells(Rows.Count, 4).End(xlUp).Row
  For i = 2 To r
    If (sheetn.Cells(i, 4).Value = "") Then
      GoTo a1:
    End If
    filename = sheetn.Cells(i, 3).Text
    If (filename = "") Then filename = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
    Dim result
    result = Dir(sheetn.Cells(i, 2).Text, vbDirectory)
    If (directory <> "" Or result <> True) Then sPath = sheetn.Cells(i, 2).Text
    Call toJPG(sPath, filename, sheetn.Cells(i, 4).Text, i, sheet1)
a1:
  Next i
End Sub

Pour PNG

Option Explicit

Sub toPNG(ByVal directory, ByVal filename, ByVal url, ByVal i, ByVal sheetn As String)
  On Error GoTo myerror:
    Dim sheet1 As Worksheet
    Set sheet1 = Sheets(sheetn)
    Dim sPath As String, WSH As Variant
    Set WSH = CreateObject("WScript.Shell")
    sPath = WSH.SpecialFolders("Desktop") & "\"
    If (directory = "") Then directory = sPath
    If (Right(directory, 1) <> "\") Then directory = directory & "\"
    If (filename = "") Then filename = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
    Dim savepath As String
    savepath = directory & filename & ".png "
    Dim driver As New Selenium.ChromeDriver
    driver.SetPreference "download.default_directory", directory
    driver.SetPreference "download.directory_upgrade", True
    driver.SetPreference "download.prompt_for_download", False
    driver.SetPreference "safebrowsing.enabled", True
    driver.SetPreference "plugins.plugins_disabled", Array("Chrome PDF Viewer")
    driver.AddArgument "headless"
    driver.AddArgument "disable-gpu"
    driver.AddArgument "hide-scrollbars"
    Dim w As Long
    Dim h As Long
    
    driver.Start
    driver.Get url
    driver.FindElementByClass("tab02").Click
    driver.ExecuteScript ("this.document.getElementById('tab01').setAttribute('class','tabContent01');")
    driver.ExecuteScript ("this.document.getElementById('tab03').setAttribute('class','tabContent03');")
    w = driver.ExecuteScript("return document.body.scrollWidth")
    h = driver.ExecuteScript("return document.body.scrollHeight")
    driver.Window.SetSize w, h
    driver.TakeScreenshot.SaveAs savepath
    sheet1.Cells(i, 5).Value = 1
    sheet1.Cells(i, 6).Value = savepath
    driver.Quit
    Exit Sub
myerror:
    
    sheet1.Cells(i, 5).Value = 0
End Sub
Sub dopng()
  Dim sPath As String, WSH As Variant
    Set WSH = CreateObject("WScript.Shell")
    sPath = WSH.SpecialFolders("Desktop") & "\"
    Dim directory As String
    Dim filename As String
    directory = sPath
    If (Right(Len(directory), 1) <> "\") Then directory = directory & "\"
    
  Dim sheet1 As String
  Dim i As Long
  
  sheet1 = "Pour png"
  
  Dim sheetn As Worksheet
  Set sheetn = Sheets(sheet1)
  Dim r As Long
  r = sheetn.Cells(Rows.Count, 4).End(xlUp).Row
  For i = 2 To r
    If (sheetn.Cells(i, 4).Value = "") Then
      GoTo a1:
    End If
    filename = sheetn.Cells(i, 3).Text
    If (filename = "") Then filename = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
    Dim result
    result = Dir(sheetn.Cells(i, 2).Text, vbDirectory)
    If (directory <> "" Or result <> True) Then sPath = sheetn.Cells(i, 2).Text
    Call toPNG(sPath, filename, sheetn.Cells(i, 4).Text, i, sheet1)
a1:
  Next i
End Sub

La structure du livre se compose de trois feuilles «pdf», «png» et «jpg».

Colonnes B, C, D après la deuxième ligne Entrez la destination d'enregistrement, le nom d'enregistrement et l'URL Il est enregistré lorsque vous déplacez la macro.

Au fait, vous pouvez également manipuler le DOM avec driver.executescirpts etc., afin de pouvoir capturer la page Web après avoir joué avec.

somnolent. .. .. Aussi, je joindrai une photo plus tard.

Recommended Posts

Comment convertir des pages Web en PDF, PNG, JPG avec VBA (Excel) (Selenium Basic)
Convertir des fichiers PDF en fichiers PNG avec GIMP
Convertir un PDF A4 en A3 toutes les 2 pages
Convertir un PDF en image avec ImageMagick
Activé pour convertir PNG en JPG avec Pillow of Python
Convertir de PDF en CSV avec pdfplumber
Convertir des données Excel en JSON avec python
Conversion DICOM en PNG avec ordre croissant et décroissant
[Python] Comment lire des fichiers Excel avec des pandas
Convertir un fichier svg en png / ico avec Python
Convertissez plusieurs fichiers jpg en un seul fichier PDF
Comment convertir (32,32,3) en tenseur à 4 dimensions (1,32,32,1) avec le type ndarray
Connexion automatique au service maintenant avec le pilote Web Selenium
Comment convertir / restaurer une chaîne avec [] en python
Comment convertir des données détenues horizontalement en données détenues verticalement avec des pandas
Comment convertir un objet de classe en dictionnaire avec SQLAlchemy
Comment convertir un fichier JSON en fichier CSV avec Python Pandas
Comment convertir des fichiers Json au format CSV ou au format EXCEL
Comment passer en mode smartphone avec Python + Selenium + Chrome
Remarques sur la façon d'utiliser Firefox avec du sélénium sous Windows
Convertir 202003 en 2020-03 avec les pandas
Comment déboguer le sélénium
Convertir json en Excel
Comment déployer une application Web créée avec Flask sur Heroku
Comment mettre un lien hypertexte vers "file: // hogehoge" avec sphinx-> pdf
Comment convertir un tableau en dictionnaire avec Python [Application]
Comment sortir un document au format pdf avec Sphinx
Comment ne pas charger d'images lors de l'utilisation de PhantomJS avec Selenium
Comment manipuler le DOM dans iframe avec Selenium
Convertissez des images numérisées déformées en PDF avec Pillow et PyPDF
Introduction à Python pour les utilisateurs de VBA - Appeler Python depuis Excel avec xlwings -
Comment gratter en quelques secondes avec le sélénium de Python
Comment lire un fichier Excel (.xlsx) avec Pandas [Python]