LoginSignup
0
0

More than 3 years have passed since last update.

python 通常使うプリンタ名の取得方法がわからない。

Last updated at Posted at 2020-05-21

PYTHONで「通常使うプリンタ名」(デフォルトプリンタ)の取得方法がわからない。

下記のプログラムは「通常使うプリンタ」を「違うプリンタ」へ変更のプログラムです。→現在の「通常使うプリンタ名」を取得したい!

「通常使うプリンタ名」の取得方法について、ご教授よろしくお願いいたします。

下記のプログラムは「通常使うプリンタ」を「"Microsoft Print to PDF"」のプリンタへ変更するプログラム。(pythonからコマンドプロントを叩いてます)

プリンタ変更プログラム.py
import subprocess
cmd = 'rundll32.exe printui.dll,PrintUIEntry /y /n "Microsoft Print to PDF"'
returncode = subprocess.call(cmd)

VBSで「通常使うプリンタ名の取得」のコードがネットにあり(URL:https://ekafp7.blogspot.com/2016/12/blog-post.html)
同様に下記のようにPYTHONでアレンジして作成したが、4行目でエラーがでる。
objClassSet = objService.ExecQuery("Select * From Win32_Printer")
おそらく引数がだめなのかよくわかりませんがエラーがでます。

通常で使うプリンタ取得プログラム.py
import win32com.client
objLocator = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objService = objLocator.ConnectServer
objClassSet = objService.ExecQuery("Select * From Win32_Printer")

for objClass in objClassSet:
    if objClass.Default:
        strName = objClass.Caption
 print(strName)

また、プリンタ一覧は下記のプログラムででましたが、「通常使うプリンタ名」までは出てこない。。

プリンタ一覧プログラム.py
import win32com.client
o = win32com.client.Dispatch("WScript.Network")
prlist = o.EnumPrinterConnections()
for pr in prlist:
    print(pr)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0