Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Open a PDF File in Python?

#1
How to Open a PDF File in Python?

In this tutorial, you’ll learn how to open a PDF file in an external PDF reader or the browser, using a simple Python command. There are many variants to this problem, so let’s dive right into the different ones—and their solutions!



Method 1: Open PDF Standard Viewer with os.system(path) — With CMD


You can open a PDF file in your standard PDF program such as Adobe Acrobat Reader using the command os.system(path) using the os module and the path string to the PDF file. This opens a command-line shell as an intermediate operating system program that in turn opens the PDF.

import os
path = 'my_file.pdf'
os.system(path)
https://blog.finxter.com/wp-content/uplo...00x143.png 300w, https://blog.finxter.com/wp-content/uplo...68x366.png 768w, https://blog.finxter.com/wp-content/uplo...36x732.png 1536w, https://blog.finxter.com/wp-content/uplo...ge-106.png 1801w" sizes="(max-width: 1024px) 100vw, 1024px" />

Method 2: Open PDF Standard Viewer with subprocess.Popen() — Without CMD


If you want to open a PDF file in the standard PDF viewer such as Adobe Acrobat Reader, you can use the subprocess.Popen([path], shell=True) command. This doesn’t open an intermediary command line prompt but opens the PDF directly in the viewer.

# Method 2: Open with subprocess
import subprocess
path = 'my_file.pdf'
subprocess.Popen([path], shell=True)
https://blog.finxter.com/wp-content/uplo...00x123.png 300w, https://blog.finxter.com/wp-content/uplo...68x315.png 768w, https://blog.finxter.com/wp-content/uplo...36x629.png 1536w, https://blog.finxter.com/wp-content/uplo...ge-107.png 1608w" sizes="(max-width: 1024px) 100vw, 1024px" />

Method 3: Open PDF Standard Program with webbrowser.open_new()


If you want to open a PDF file in the standard PDF viewer such as Adobe Acrobat Reader, you can use the webbrowser.open_new(path) command. This doesn’t open an intermediary command line prompt but opens the PDF directly in the viewer.

# Method 3: Open with webbrowser
import webbrowser
path = 'my_file.pdf'
webbrowser.open_new(path)
https://blog.finxter.com/wp-content/uplo...00x119.png 300w, https://blog.finxter.com/wp-content/uplo...68x304.png 768w, https://blog.finxter.com/wp-content/uplo...36x607.png 1536w, https://blog.finxter.com/wp-content/uplo...ge-108.png 1670w" sizes="(max-width: 1024px) 100vw, 1024px" />

Method 4: Open PDF with Python Given an URL


If you want to open a PDF file in the standard web browser, given any URL to the PDF, you can use the webbrowser.open_new(url) command.

# Method 4: Open with Webbrowser
import webbrowser
path = 'https://blog.finxter.com/wp-content/uploads/2019/02/CheatSheet-Python-2_-Data-Structures.docx.pdf'
webbrowser.open_new(path)
https://blog.finxter.com/wp-content/uplo...00x108.png 300w, https://blog.finxter.com/wp-content/uplo...68x276.png 768w, https://blog.finxter.com/wp-content/uplo...36x553.png 1536w, https://blog.finxter.com/wp-content/uplo...ge-109.png 1915w" sizes="(max-width: 1024px) 100vw, 1024px" />

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post How to Open a PDF File in Python? first appeared on Finxter.



https://www.sickgaming.net/blog/2021/02/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Read a File Without Newlines xSicKxBot 0 24 02-26-2021, 06:41 AM
Last Post: xSicKxBot
  [Tut] Python Get Milliseconds xSicKxBot 0 41 02-19-2021, 08:28 AM
Last Post: xSicKxBot
  [Tut] Python str() Function xSicKxBot 0 36 02-14-2021, 06:05 AM
Last Post: xSicKxBot
  [Tut] Python issubclass() xSicKxBot 0 42 02-12-2021, 10:40 AM
Last Post: xSicKxBot
  [Tut] Python int() Function xSicKxBot 0 47 02-04-2021, 08:21 PM
Last Post: xSicKxBot
  [Tut] How To Cut A String In Python? xSicKxBot 0 36 02-01-2021, 04:09 PM
Last Post: xSicKxBot
  [Tut] Python filter() xSicKxBot 0 56 02-01-2021, 03:25 AM
Last Post: xSicKxBot
  [Tut] How To Run A File In The IPython Console As Default Instead Of Terminal? xSicKxBot 0 37 01-26-2021, 10:28 AM
Last Post: xSicKxBot
  [Tut] How to Check Whether a File Exists Without Exceptions? xSicKxBot 0 36 01-23-2021, 05:56 AM
Last Post: xSicKxBot
  [Tut] Python help() xSicKxBot 0 41 01-22-2021, 01:02 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
(Indie Deal) Gloria Victis, Crackerjack ...
Last Post: xSicKxBot
Today 06:00 AM
» Replies: 0
» Views: 3
AppleInsider - iPhone 11 reunited with o...
Last Post: xSicKxBot
Today 06:00 AM
» Replies: 0
» Views: 3
News - Video: Beautiful Ghibli-Esque Pla...
Last Post: xSicKxBot
Today 06:00 AM
» Replies: 0
» Views: 3
News - Fortnite: Ant-Man Will Be The Nex...
Last Post: xSicKxBot
Today 06:00 AM
» Replies: 0
» Views: 3
Haven | A tale of two lover who gave up ...
Last Post: SickProdigy
Today 12:32 AM
» Replies: 0
» Views: 6
News - Hate Zips? Then This Magnet-Power...
Last Post: xSicKxBot
Yesterday 08:17 PM
» Replies: 0
» Views: 3
News - Fortnite: Ant-Man Will Be The Nex...
Last Post: xSicKxBot
Yesterday 08:17 PM
» Replies: 0
» Views: 3
News - The Case of the Missing Rewards
Last Post: xSicKxBot
Yesterday 05:31 PM
» Replies: 0
» Views: 2
Xbox Wire - Minecraft Dungeons Flames of...
Last Post: xSicKxBot
Yesterday 05:30 PM
» Replies: 0
» Views: 3
News - Don’t Miss: A 2018 economic deep ...
Last Post: xSicKxBot
Yesterday 05:30 PM
» Replies: 0
» Views: 5

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020