簡體   English   中英

在 chrome 中自動打印/保存網頁為 pdf - python 3.6

[英]Automate print/save web page as pdf in chrome - python 3.6

我正在嘗試創建一個腳本,通過 Chrome 的打印功能自動保存只讀 pdf,以將其另存為同一文件夾中的另一個 pdf。 這將刪除“只讀”功能。 但是,在運行腳本時,我不確定在哪里可以指定我自己的特定目標文件夾,腳本將其直接保存在下載文件夾中。

以下代碼的完整道具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();')

好的,我想我找到了解決方案。 只需添加以下代碼行:

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

它仍然不理想,因為您無法指定所需的新文件名,但現在可以使用。

如果有人有更好的解決方案,請在此處發布。 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM