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

邮差的信 提交于 2019-12-31 05:42:27

问题


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 running it. I have to convert around 200K+ files and have to work on something else simultaneously. But its really not letting me to work on anything else.

Is there a way of stopping this or running it in background? I really appreciate your help.

PS. Sorry I am new to it, don't know any other way to stop it.

I am using wkhtmltox-0.12.2.2_msvc2013-win64 software


回答1:


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.




回答2:


  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)


来源:https://stackoverflow.com/questions/29658207/how-to-stop-wkhtmltopdf-exe-pop-up-while-running-it-with-python

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