如何使用 Selenium + Chromedriver 更改保存的 PDF 页面名称
How to change saved PDF page name with Selenium + Chromedrive
我有一个使用 Selenium Python 下载 PDF 页面的脚本 made based on this question
我目前的目标是更改此文件的名称,使其位于我选择的名称中,然后更改保存文件的目标文件夹。
我的疑惑是:
我应该在哪里更改以便文件以我选择的名称保存?
即使对“prefs=”变量进行了更改,文件仍会继续保存在默认 chrome 目录中。
目前我有以下代码:
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')
将 download.default_directory
更改为 savefile.default_directory
以便您的保存位置有效。
遗憾的是我认为我们无法在下载前更改文件名,但您可以,通过重命名下载文件夹中的最新文件:
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"))
我有一个使用 Selenium Python 下载 PDF 页面的脚本 made based on this question
我目前的目标是更改此文件的名称,使其位于我选择的名称中,然后更改保存文件的目标文件夹。
我的疑惑是: 我应该在哪里更改以便文件以我选择的名称保存?
即使对“prefs=”变量进行了更改,文件仍会继续保存在默认 chrome 目录中。
目前我有以下代码:
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')
将 download.default_directory
更改为 savefile.default_directory
以便您的保存位置有效。
遗憾的是我认为我们无法在下载前更改文件名,但您可以
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"))