Print an image file to a specified printer on Windows. Uses Python subprocess + mspaint /pt to avoid bash/git-bash Chinese path encoding problems.
python -c "import os; print(os.path.exists(r'<absolute_path>'))"
powershell -Command "Get-Printer | Select-Object Name, PrinterStatus | Format-Table -AutoSize"
Check default printer:
powershell -Command "(Get-WmiObject -Query 'SELECT * FROM Win32_Printer WHERE Default=True').Name"
python -c "import subprocess; img = r'<absolute_path>'; prn = '<printer_name>'; subprocess.run(['mspaint', '/pt', img, prn])"
Critical: Use Python raw strings (r'...') for paths — this is what makes it work! Bash's cmd /c mspaint /pt fails with Chinese paths due to encoding corruption, but Python's raw strings preserve the exact bytes.
python -c "import subprocess; img = r'E:\私域相关资料\南京泊宁资料\8888.jpg'; prn = 'Canon G3000 series Printer (副本 1)'; subprocess.run(['mspaint', '/pt', img, prn])"
cmd /c mspaint /p or cmd /c mspaint /pt from bash — Chinese path encoding gets mangled.
powershell Start-Process -Verb Print — it pops up a dialog requiring manual confirmation.
r'...' raw strings for paths.
mspaint /pt takes: mspaint /pt — just 2 params is enough, no need for driver/port.
returncode: 0 = print job sent successfully.
共 1 个版本