0. Learn that win32print needs to be installed to connect python to the printer, go to the official website to check https://github.com/mhammond/pywin32/releases
1. Select the correct version of python. After the download is successful, you will get the installation package. Click Install to display python version 3.5 required, which was not found in the registry
2. The solution is to create a new python script, named register.py, the content is:
import sys
from winreg import *
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)
def RegisterPy():
try:
reg = OpenKey(HKEY_CURRENT_USER, regpath)
except EnvironmentError as e:
try:
reg = CreateKey(HKEY_CURRENT_USER, regpath)
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
except:
print("*** Unable to register!")
return
print("--- Python", version, "is now registered!")
return
if (QueryValue(reg, installkey) == installpath and
QueryValue(reg, pythonkey) == pythonpath):
CloseKey(reg)
print("=== Python", version, "is already registered!")
return
CloseKey(reg)
print("*** Unable to register!")
print("*** You probably have another Python installation!")
if __name__ == "__main__":
RegisterPy()
Just run it and show success! Next step.
3. Connect the printer, when you want to print a single line, you can send the text directly to the printer like this
import os, sys
import win32print
# coding=utf-8
printer_name = win32print.GetDefaultPrinter ()
if sys.version_info >= (3,):
raw_data =bytes ("This is a test", "utf-8")
else:
raw_data = "This is a test"
hPrinter = win32print.OpenPrinter (printer_name)
try:
hJob = win32print.StartDocPrinter (hPrinter, 1, ("test of raw data", None, "RAW"))
try:
win32print.StartPagePrinter (hPrinter)
win32print.WritePrinter (hPrinter, str_byte)
# win32print.WritePrinter (hPrinter, raw_data+'hhh')
win32print.EndPagePrinter (hPrinter)
finally:
win32print.EndDocPrinter (hPrinter)
finally:
win32print.ClosePrinter (hPrinter)
Make sure that the printer can print after it is turned on, which is almost the result
But my requirement is to print multiple lines, so I tried to expand the characters with a loop statement
import os, sys
import win32print
# coding=utf-8
printer_name = win32print.GetDefaultPrinter ()
#
# raw_data could equally be raw PCL/PS read from
# some print-to-file operation
#
# def get_Print(raw_data):
code_list=[1153000235,1153000236,1153000237]
if sys.version_info >= (3,):
s_tp=''
raw_data = " computer\n {}\n it\n lee\n"
for co in code_list:
# print(raw_data.format(co))
s_tp=s_tp+raw_data.format(co)
# print(s_tp)
str_byte=bytes (s_tp, "utf-8")
# print('this')
else:
raw_data = "This is a test"
hPrinter = win32print.OpenPrinter (printer_name)
try:
hJob = win32print.StartDocPrinter (hPrinter, 1, ("test of raw data", None, "RAW"))
try:
win32print.StartPagePrinter (hPrinter)
win32print.WritePrinter (hPrinter, str_byte)
# win32print.WritePrinter (hPrinter, raw_data+'hhh')
win32print.EndPagePrinter (hPrinter)
finally:
win32print.EndDocPrinter (hPrinter)
finally:
win32print.ClosePrinter (hPrinter)
But the printed effect is
It can be seen that the function of the newline character'\n' here is to directly wrap the line to the next line corresponding to the function character. I originally thought it would be printed like this
computer
1153000235
it
lee
computer
1153000236
it
lee
computer
1153000236
it
lee
But it is actually:
computer
1153000235
it
lee
computer
1153000236
it
lee
computer
1153000236
it
lee
Because the back is too wide, I didn’t type it out
I didn’t find the newline character in the api.
So I took another idea, send the text to the word document, and then print the document, using the python-docx library, if not, you can install it directly with pip
# coding:utf-8
import win32print
import win32api
from docx import Document
from docx.shared import Inches
document = Document()
code_list=[1153000235,1153000236,1153000237]
for i in range(3):
testName = "computer\n"+str(code_list[i])+"\n"+"IT\n"+"Lee"
p_total = document.add_heading("", 2)
r_total = p_total.add_run(testName)
r_total.font.bold = True
# document.add_picture(img_name, width=Inches(1.5)) # 向文档里添加图片
document.save("E:/test.docx") # 保存文档
fn="E:/test.docx"
win32api.ShellExecute(0,'print',fn,win32print.GetDefaultPrinterW(),".",0)
Got this
It’s almost like this. I personally think it’s more convenient to pass characters directly. I’ll study that method later.