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

前端 未结 2 1745
自闭症患者
自闭症患者 2021-01-26 00:20

I am using wkhtmltopdf.exe for converting html to pdf using python. The pop-up window of wkhtmltopdf.exe making it very difficult for me to work on any other things while runnin

相关标签:
2条回答
  • 2021-01-26 01:08

    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.

    0 讨论(0)
  • 2021-01-26 01:20
    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)
    
    0 讨论(0)
提交回复
热议问题