Search code examples
pythonpdfpreview

How to preview pdf file in browser using Python?


I want to preview a pdf file in browser then rename that file and then mail it to someone. Is it possible to do this with python script?


Solution

  • There is a module to use the browser

    import webbrowser
    new = 2 # open in a new tab, if possible
    
    # open a public URL, in this case, the webbrowser docs
    url = "http://docs.python.org/library/webbrowser.html"
    webbrowser.open(url,new=new)
    
    # open an HTML file on my own (Windows) computer
    url = "file://X:/MiscDev/language_links.html"
    webbrowser.open(url,new=new)
    

    Source of the example here; documentation here. It should work the same way with Pdf files instead than html, if the browser can open them.

    If you want to pause while you preview it, you can use

    wait = input("PRESS ENTER TO CONTINUE.")
    

    Renaming has an answer here, you use os.rename(src, dst).

    Emailing has been answered here, documentation here.

    Now put it all together!