如下程式为使用Python捞取Codesoft 标签模板的文字坐标和变量名,可以作为其他二次开发的范例。
import win32com.client as winc
import os
import ctypes
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
from tkinter import filedialog
global c1,d1,e1,t1,s2fname,t2,t3
def openfiles2():
t2.delete(0,END)
s2fname = filedialog.askopenfilename(title='选择文件', filetypes=[('bom', '*.lab'),('bom', '*.LAB'),('All Files', '*')])
return s2fname
def finddim():
my_printer = winc.DispatchEx("lppx2.Application") # 调用CodeSoft软件的核心组件
# 无传参式打印
my_lab_doc = my_printer.Documents.Open(os.path.normpath(t2.get()),False) # 实例打开.lab文件'''
#my_lab_doc.PrintDocument(1) # 传输该.lab文件的模板内容和打印指令给打印机 Function参数为打印张数
#my_lab_doc.FormFeed
#for vari in my_lab_doc.Variables.FormVariables: #变量名
#print(vari.Name) 显示变量名
#print(my_lab_doc.Variables.FormVariables) 显示变量内容
for text in my_lab_doc.DocObjects.Texts:
print (text.value,':',text.Left/100,',',text.top/100)
t1.insert(END,(text.value+'| '+'X: '+str(text.Left/100)+' '+'Y: '+str(text.top/100)))
t1.insert(END,'\n')
def findzuobiao():
my_printer = winc.DispatchEx("lppx2.Application") # 调用CodeSoft软件的核心组件
# 无传参式打印
my_lab_doc = my_printer.Documents.Open(os.path.normpath(t2.get()),False) # 实例打开.lab文件'''
for vari in my_lab_doc.Variables.FormVariables: #变量名
print(vari.Name)
t1.insert(END,vari.Name)
t1.insert(END,'\n')
def button_a():
file1=openfiles2()
#ojec(file1)
#findline(file1)
t2.insert('',file1)
return file1
def button_b():
file2=openfiles2()
#findcol(file2)
t3.insert('',file2)
return file2
def button_c():
#file1=t2.get()
finddim()
print('OK')
def button_d():
#file1=t2.get()
findzuobiao()
print('OK')
def clean_text():
t1.delete('1.0','end')
t2.delete('1.0','end')
t3.delete('1.0','end')
ctypes.windll.shcore.SetProcessDpiAwareness(1)
ScaleFactor=ctypes.windll.shcore.GetScaleFactorForDevice(0)
root = tk.Tk()
root.tk.call('tk', 'scaling', ScaleFactor/75)
root.title("Read Codesoft by 庞霄")
root.geometry('800x500+200+10')
#解决分辨率问题
a=Button(root, text='Template',command=button_a)
a.grid(row=0,column=0,sticky=E+W)
t2=Entry(root)
t2.grid(row=1,column=0,sticky=E+W)
#t3=Entry(root)
#t3.grid(row=1,column=1,sticky=W)
l1=Label(root,text='模板内容:')
l1.grid(row=2,column=0)
t1=Text(root,width=80,height=15)
t1.grid(row=3,column=0,sticky=S)
s1=Scrollbar(root,orient=tk.VERTICAL)
t1.configure(yscrollcommand=s1.set,font='Times 9')
s1.grid(row=3,column=1,sticky=S+W+E+N)
s1.configure(command=t1.yview)
f=Button(root, text='内容坐标',command=button_c)
f.place(x=0,y=410)
#f.grid(row=4,column=0,sticky=W)
gg=Button(root, text='清除',command=clean_text)
gg.place(x=0,y=445)
#gg.grid(row=4,column=1,sticky=W)
gg=Button(root, text='变量名',command=button_d)
gg.place(x=140,y=410)
root.mainloop()