如何禁用页眉和页脚打印

2024-09-30 14:15:54 发布

您现在位置:Python中文网/ 问答频道 /正文

有人知道如何在使用selenium打印时禁用“页眉和页脚”选项吗?默认设置为true,有人知道如何修复吗?谢谢你

import json
import os
from selenium import webdriver

# setting html path
htmlPath = os.getcwd() + "\\sample.html"
addr = "file:///" + htmlPath

# setting Chrome Driver
chromeOpt = webdriver.ChromeOptions()
appState = {
   "recentDestinations": [
        {
            "id": "Save as PDF",
            "origin": "local",
            "account": ""
        }
    ],
    "selectedDestinationId": "Save as PDF",
    "version": 2
}
prefs = {
    'printing.print_preview_sticky_settings.appState': json.dumps(appState)}
chromeOpt.add_experimental_option('prefs', prefs)
chromeOpt.add_argument('--kiosk-printing')
driver = webdriver.Chrome('.\\bin\\chromedriver', options=chromeOpt)

# HTML open and print
driver.get(addr)
driver.execute_script('return window.print()')```

Tags: importjsonoshtmldriverseleniumchromesetting
1条回答
网友
1楼 · 发布于 2024-09-30 14:15:54

只需将"isHeaderFooterEnabled": False添加到appstate下面的更改中即可

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

屏幕截图:注释了chromeOpt.add_argument(' kiosk-printing')行,只是为了在屏幕截图中显示取消选中的选项。 enter image description here

您可以在下面的页面上找到有关chromium选项的详细信息

https://github.com/chromium/chromium/blob/eadef3f685cd9e96e94fcb9645b6838b6d0907a8/chrome/browser/resources/print_preview/data/model.js

enter image description here

相关问题 更多 >

    热门问题