gpt4 book ai didi

python - 如何使用 Selenium 为 PDF 打印指定打印位置

转载 作者:行者123 更新时间:2023-12-05 05:42:24 33 4
gpt4 key购买 nike

无论我做什么,文件都会不断打印到我的下载(Windows 默认)文件夹中,而不是指定的文件夹中。我做了我的研究,显然应该使用 savefile.default_directory 选项而不是 download.default_directory 但它无论如何都不起作用。我尝试从路径中删除尾随 \\ 但没有成功。如果有任何不同,这是在工作 PC 上,Windows 10 机器上。

   import os
os.environ["PATH"] += os.pathsep + r'C:\Program Files (x86)\Chromedriver99';

from selenium.webdriver.chrome.options import Options
from selenium import webdriver

options = Options()
options.add_experimental_option(
"prefs",
{
"download.prompt_for_download": False,
"profile.default_content_setting_values.automatic_downloads": 1,
"download.default_directory": r"C:\Users\Lucas\Downloads\ECV\\",
"savefile.default_directory": r"C:\Users\Lucas\Downloads\ECV\\",
"download.directory_upgrade": True,
"safebrowsing.enabled": True # Some answers include this, makes no difference
},
)
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)

# PDF printing settings
print_settings = {
"recentDestinations": [{
"id": "Save as PDF",
"origin": "local",
"account": "",
}],
"selectedDestinationId": "Save as PDF",
"version": 2,
"isHeaderFooterEnabled": False,
"isLandscapeEnabled": True
}

prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(print_settings)}
options.add_experimental_option('prefs', prefs)
options.add_argument('--kiosk-printing') # Some answers include this, makes no difference


driver = webdriver.Chrome(options=options)
driver.get('https://stackoverflow.com/')
driver.execute_script('window.print();')

最佳答案

您有两个问题,第一个是您设置了两次 prefs,并且由于 add_experimental_option() 使用字典作为选项,第二个设置覆盖了第一个,所有这些设置实际上都被删除了。

self._experimental_options = {}


def add_experimental_option(self, name, value):
"""
Adds an experimental option which is passed to chrome.

Args:
name: The experimental option name.
value: The option value.
"""
self._experimental_options[name] = value

第二个问题是在路径上使用原始字符串 r,它的计算结果为 C:\Users\Lucas\Downloads\ECV\\\\ 这是无效的。使用 / 而不是 \ "C:/Users/Lucas/Downloads/ECV/" 或使用 \\ 没有 r "C:\\Users\\Lucas\\Downloads\\ECV\\"

print_settings = {
"recentDestinations": [{
"id": "Save as PDF",
"origin": "local",
"account": "",
}],
"selectedDestinationId": "Save as PDF",
"version": 2,
"isHeaderFooterEnabled": False,
"isLandscapeEnabled": True
}

prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(print_settings),
"download.prompt_for_download": False,
"profile.default_content_setting_values.automatic_downloads": 1,
"download.default_directory": "C:\\Users\\Lucas\\Downloads\\ECV\\",
"savefile.default_directory": "C:\\Users\\Lucas\\Downloads\\ECV\\",
"download.directory_upgrade": True,
"safebrowsing.enabled": True}

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

driver = webdriver.Chrome(options=options)
driver.get('https://stackoverflow.com/')
driver.execute_script('window.print();')

关于python - 如何使用 Selenium 为 PDF 打印指定打印位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72052662/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com