excel_base_func.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # from PySide6.QtWidgets import QApplication
  2. from PIL import Image
  3. import settings
  4. import time
  5. import os
  6. def get_clip_image(image_path):
  7. try:
  8. # cb = QApplication.clipboard()
  9. # if cb.mimeData().hasImage():
  10. # qt_img = cb.image()
  11. # pil_img = Image.fromqimage(qt_img) # 转换为PIL图像
  12. # if pil_img.width > 10:
  13. # pil_img = pil_img.convert("RGB")
  14. # pil_img.save(image_path)
  15. # print("图片剪切板保存成功:{}".format(image_path))
  16. # return True
  17. return False
  18. except BaseException as e:
  19. print(e)
  20. settings.logger.info("获取剪切板图片异常:{}".format(e))
  21. return False
  22. def get_one_cell_shape_image(shape, image_out_path):
  23. try:
  24. flag = True
  25. _address = shape.TopLeftCell.Address
  26. print("_address", _address) # $E$2
  27. shape.LockAspectRatio = True
  28. old_w = shape.Width
  29. shape.Width = 500
  30. print("shape.Copy")
  31. shape.Copy()
  32. time.sleep(0.1)
  33. if not get_clip_image(image_out_path):
  34. flag = False
  35. shape.Width = old_w
  36. except BaseException as e:
  37. print("get_one_cell_shape_image", e)
  38. return False
  39. return flag
  40. def add_pic_with_wps(sheet, row, column, image_path, pic_w=35):
  41. cell = sheet.Cells(row, column)
  42. cell_value = cell.Value
  43. if cell_value:
  44. if "DISPIMG" in cell_value:
  45. print("已有图片")
  46. return
  47. cell.ColumnWidth = 10
  48. cell.RowHeight = 42
  49. im = Image.open(image_path)
  50. w = pic_w
  51. h = int((im.height * w) / im.width)
  52. # pic = sheet.Shapes.AddPicture(FileName=image_path, LinkToFile=False, SaveWithDocument=True,
  53. # Left=cell.Left + 2, Top=cell.Top + 2, Width=-1, Height=-1)
  54. # https://learn.microsoft.com/zh-tw/office/vba/api/excel.shapes.addpicture
  55. pic = sheet.Shapes.AddPicture(image_path, 0, 1, cell.Left + 2, cell.Top + 2, w, h)
  56. pic.LockAspectRatio = True
  57. pic.Placement = 1 # 随单元格大小变化
  58. # os.remove(image_path)
  59. # r = pic.ShapeRange
  60. # r.LockAspectRatio = True
  61. def add_pic_with_office(sheet, row, column, image_path, pic_w=35):
  62. # self.add_pic_with_wps(sheet, row, column, image_path, pic_w=35)
  63. # return
  64. cell = sheet.Cells(row, column)
  65. cell_value = cell.Value
  66. if cell_value:
  67. if "DISPIMG" in cell_value:
  68. print("已有图片")
  69. return
  70. cell.ColumnWidth = 10
  71. cell.RowHeight = 42
  72. im = Image.open(image_path)
  73. w = pic_w
  74. h = int((im.height * w) / im.width)
  75. pic_shape = sheet.Shapes.AddShape(1, cell.Left + 2, cell.Top + 2, w, h)
  76. pic_shape.Fill.UserPicture(image_path)
  77. pic_shape.Line.Weight = 0
  78. pic_shape.Placement = 1 # 随单元格大小变化
  79. def get_all_row_sheet_images(sheet):
  80. """
  81. 返回每行中是否存在形状,第二行为index=0
  82. """
  83. row_address_data_dict = {}
  84. for i, shape in enumerate(sheet.Shapes):
  85. try:
  86. _address = shape.TopLeftCell.Address
  87. _row = 0
  88. if isinstance(_address, str):
  89. _row = _address.split("$")[2]
  90. _row = int(_row) - 2
  91. if _row == -1:
  92. break
  93. row_address_data_dict[_row] = shape
  94. except BaseException as e:
  95. print(e)
  96. break
  97. return row_address_data_dict
  98. def save_log_text():
  99. # 移除30条以外的文件
  100. f_list = []
  101. for new_file_name in os.listdir(r"log\upload_log"):
  102. _file_path = "{}\{}".format(r"log\upload_log", new_file_name)
  103. f_list.append({"file_name": new_file_name,
  104. "file_path": _file_path,
  105. "create_time": os.path.getctime(_file_path)
  106. })
  107. if f_list:
  108. f_list.sort(key=lambda x: x["create_time"], reverse=True)
  109. while 1:
  110. if len(f_list) > 30:
  111. del_file_dict = f_list.pop()
  112. del_file_path = "{}\{}".format(os.getcwd(), del_file_dict["file_path"])
  113. os.remove(del_file_path)
  114. else:
  115. break
  116. pass