【发布时间】:2018-09-13 12:52:57
【问题描述】:
我正在尝试创建一个脚本,以通过 Chrome 的打印功能自动保存只读 pdf,以将其另存为同一文件夹中的另一个 pdf。这将删除“只读”功能。但是,在运行脚本时,我不确定在哪里可以指定自己的特定目标文件夹,并且脚本将其直接保存在 Downloads 文件夹中。
以下代码的完整道具https://stackoverflow.com/users/1432614/ross-smith-ii。
任何帮助将不胜感激。
import json
from selenium import webdriver
downloadPath = r'mypath\downloadPdf\\'
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local"
}
],
"selectedDestinationId": "Save as PDF",
"version": 2
}
profile = {'printing.print_preview_sticky_settings.appState':json.dumps(appState)}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', profile)
chrome_options.add_argument('--kiosk-printing')
driver = webdriver.Chrome(chrome_options=chrome_options)
pdfPath = r'mypath\protected.pdf'
driver.get(pdfPath)
driver.execute_script('window.print();')
【问题讨论】:
-
听起来不错,我会收回flag。
-
对我来说,
driver.execute_script('window.print();')只是关闭窗口,没有下载任何内容。
标签: python google-chrome selenium pdf-generation