【问题标题】:Automate print/save web page as pdf in chrome - python 3.6在 chrome 中自动打印/保存网页为 pdf - python 3.6
【发布时间】: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();')

【问题讨论】:

标签: python google-chrome selenium pdf-generation


【解决方案1】:

好的,我想我找到了解决方案。只需在下面的代码中添加以下行:

profile = {'printing.print_preview_sticky_settings.appState':json.dumps(appState),'savefile.default_directory':downloadPath}

这仍然不理想,因为您无法指定所需的新文件名,但现在可以使用。

如果有人有更好的解决方案,请在此处发布。谢谢

【讨论】:

  • 替换此行会自动下载文件
猜你喜欢
  • 2015-09-17
  • 2011-12-28
  • 1970-01-01
  • 2014-12-05
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
  • 2020-01-13
  • 1970-01-01
相关资源
最近更新 更多