1

I have a script that uses Selenium Python to download a PDF page made based on this question

My goal at the moment is to change the name of this file so that it is located with the name I chose and then change the destination folder of the saved file.

My doubts are: Where should I change so that the file is saved with the name I choose?

Even with the changes made to the "prefs=" variable, the file continues to be saved in the default chrome directory.

At the moment I have the following code:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless') # Escondendo o navegador
driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()
settings = {
    "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local",
            "account": "",
        }],
        "isLandscapeEnabled": True,
        "selectedDestinationId": "Save as PDF",
        "version": 2,
    }

prefs = {
    "printing.print_preview_sticky_settings.appState": json.dumps(settings),
    "profile.default_content_settings.popups" : 0,

    "download.name":"name_file", # ?????? ESTE CÓDIGO NÃO ALTERA O NOME

    "download.default_directory": r'C:\Users\diretorio_escolhido\\' # ESTE CÓDIGO NÃO ALTERA O DESTINO,

    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing.enabled": True
}


chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(name_Object.url)
sleep(5) # Pausa para carregar os dados
driver.execute_script('window.print();')

print('Gerou o PDF')

3 Answers3

2

I'm facing the same question. For now I found the following workaround:

driver.execute_script("document.title = \'{}\'".format(filename))
vimuth
  • 5,064
  • 33
  • 79
  • 116
Charles
  • 31
  • 3
0

Change download.default_directory to savefile.default_directory so your saving location works.

Sadly I think we can't change the filename before download, but you can rename your file after download, by renaming the latest file in download folder:

import os    
import shutil
download_folder = "C:\\Users\\username\\Downloads\\Test"
filename = max([download_folder + "\\" + f for f in os.listdir(Initial_path)],key=os.path.getctime)
shutil.move(filename,os.path.join(Initial_path,r"newPDFName.pdf"))
Duc Nguyen
  • 301
  • 2
  • 8
0

This answer is using vimuth's suggestion posted above, and it is fully worked for myself using Python 3.9.4

chrome_driver_path = ChromeDriverManager().install()
service = Service(chrome_driver_path)
service.start()

options = webdriver.ChromeOptions()
options.add_argument('--start-maximized')

settings = {
              "recentDestinations": [{
                    "id": "Save as PDF",
                    "origin": "local",
                    "account": "",
                }],
                "selectedDestinationId": "Save as PDF",
                "version": 2
            }
        
    prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(settings), 'savefile.default_directory': download_path }

    options.add_experimental_option('prefs', prefs)
    options.add_argument('--kiosk-printing')

    driver = webdriver.Chrome(chrome_driver_path, options=options)
Dino
  • 158
  • 1
  • 1
  • 10