How to stop wkhtmltopdf.exe pop-up while running it with Python

本小妞迷上赌 提交于 2019-12-02 10:05:28

So I solved the problem! made my python file executable using py2exe and used the .py file.

http://www.py2exe.org/index.cgi/Tutorial

It is no more popping the command prompt window.

  1. Use pythonw.exe to start your script (works with .py and .pyw extensions)
  2. or use .pyw extension for your your script (works with python.exe and pythonw.exe)

In the second case make sure the .pyw extension is handled by pythonw.exe (right click your .pyw script > Open With and make sure pythonw.exe is the default handler for .pyw files).

edit

You can also pass the CREATE_NO_WINDOW flag to the creationflags argument if you use subprocess.Popen to start your executable. This flag is a flag that can be passed to the Win32 CreateProcess API and is documented here.

Here's an example that doesn't create any console on my computer (make sure to name it with a .pyw extension) :

#!/usr/local/bin/python3
# -*- coding: utf8 -*-
import subprocess
import urllib.parse as urlparse # import urlparse for python 2.x

CREATE_NO_WINDOW = 0x08000000 # do not create a new window !

def is_url(url):
    parts = urlparse.urlsplit(url)  
    return parts.scheme and parts.netloc

def main(url, output):
    print(url, output)
    if not is_url(url):
        return -1

    proc = subprocess.Popen(["wkhtmltopdf.exe", url, output],
        creationflags=CREATE_NO_WINDOW)

    proc.wait()
    return proc.returncode


if __name__ == "__main__":
    url = "https://www.example.com"
    output = r"c:\tmp\example.pdf"

    rval = main(url, output)

    sys.exit(rval)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!