Organize_Size.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # python 读写剪切板内容, 先用下行命令安装运行库
  2. # python -m pip install pywin32
  3. import win32clipboard as w
  4. import win32con
  5. import re
  6. def getText():
  7. w.OpenClipboard()
  8. d = w.GetClipboardData(win32con.CF_TEXT)
  9. w.CloseClipboard()
  10. return(d).decode('GBK')
  11. def setText(aString):
  12. w.OpenClipboard()
  13. w.EmptyClipboard()
  14. w.SetClipboardText(aString)
  15. w.CloseClipboard()
  16. def list_to_clipboard(list):
  17. text = " ".join(list)
  18. print(text)
  19. setText(text)
  20. # 单位in或cm 换算mm
  21. def convert_mm(text, unit):
  22. list = re.findall(r"[1-9][\d\.]*\d*", text)
  23. if (unit == 'in') :
  24. print("单位英寸")
  25. for i, ch in enumerate(list):
  26. list[i] = str((int(float(ch) * 25.4 + 0.5)))
  27. elif(unit == 'cm') :
  28. print("单位厘米")
  29. for i, ch in enumerate(list):
  30. list[i] = str((int(float(ch) * 10 + 0.5)))
  31. list_to_clipboard(list)
  32. # 获取剪贴板文本
  33. text = getText()
  34. # print(text)
  35. # 正则搜索数字,写回剪贴板
  36. list = re.findall(r"[1-9][\d\.]*\d*[cmin\"]*", text)
  37. list_to_clipboard(list)
  38. # 判断厘米和英寸换算mm
  39. match = re.search(r"cm|in|\"", text)
  40. if match:
  41. # print(match.group())
  42. unit = match.group()
  43. if (unit == 'in') or (unit == '\"') :
  44. convert_mm(text, 'in')
  45. elif(unit == 'cm') :
  46. convert_mm(text, 'cm')