EXCEL_单元格中图片调整代码留存
客户需求:
1、将文件夹中信息,包含图片粘贴到汇总表中在汇总表中
2、M-T列单元格设置一样的大小,图片不保留纵横比,填充单元格。
3、将没有图片的行删除
涉及到的代码如下
# 使用提醒:
# 1. xbot包提供软件自动化、数据表格、Excel、日志、AI等功能
# 2. package包提供访问当前应用数据的功能,如获取元素、访问全局变量、获取资源文件等功能
# 3. 当此模块作为流程独立运行时执行main函数
# 4. 可视化流程中可以通过"调用模块"的指令使用此模块import xbot
from xbot import print, sleep
from .import package
from .package import variables as glvdef main(args):pass#将图片调整到单元格内
def align_pictures(excel_instance,sheet_name):# 获取底层的COM对象workbook = excel_instance.workbooktry:ws = workbook.Worksheets(sheet_name)except Exception as e:raise Exception(f"请检查sheet页名称是否正确,错误提示:{e}")adjusted_cells_list = []for shape in ws.Shapes:if shape.Type == 13: # 13对应msoPicture# 获取当前所在单元格(基于左上角)current_cell = shape.TopLeftCellcurrent_cell_top = current_cell.Topcurrent_cell_left = current_cell.Leftcurrent_cell_height = current_cell.Heightcurrent_cell_width = current_cell.Widthtarget_cell=ws.Range(current_cell.Address)# 检查图片是否超出当前单元格边界is_outside = (shape.Top < current_cell_top or shape.Left < current_cell_left or shape.Top + shape.Height > current_cell_top + current_cell_height or shape.Left + shape.Width > current_cell_left + current_cell_width)if is_outside:# 如果图片超出当前单元格,找出覆盖面积最大的单元格pic_top = shape.Toppic_left = shape.Leftpic_bottom = pic_top + shape.Heightpic_right = pic_left + shape.Widthcovered_cells = {}# 确定搜索范围(当前单元格周围一定范围内)search_range = 2 # 搜索周围5行5列的范围start_row = max(1, current_cell.Row - search_range)end_row = min(ws.Rows.Count, current_cell.Row + search_range)start_col = max(1, current_cell.Column - search_range)end_col = min(ws.Columns.Count, current_cell.Column + search_range)for row in range(start_row, end_row + 1):for col in range(start_col, end_col + 1):cell = ws.Cells(row, col)cell_top = cell.Topcell_left = cell.Leftcell_bottom = cell_top + cell.Heightcell_right = cell_left + cell.Width# 计算重叠区域overlap_top = max(pic_top, cell_top)overlap_left = max(pic_left, cell_left)overlap_bottom = min(pic_bottom, cell_bottom)overlap_right = min(pic_right, cell_right)# 如果有重叠区域if overlap_top < overlap_bottom and overlap_left < overlap_right:overlap_area = (overlap_bottom - overlap_top) * (overlap_right - overlap_left)covered_cells[cell.Address] = overlap_areaif not covered_cells:continue # 没有找到重叠单元格,跳过# 找出重叠面积最大的单元格max_cell_address = max(covered_cells, key=covered_cells.get)target_cell = ws.Range(max_cell_address)# 调整图片到目标单元格adjusted_cells_list.append(target_cell.Address)# 锁定纵横比shape.LockAspectRatio = -1 # -1对应msoTrue# 先尝试按高度调整shape.Height = target_cell.Height# 如果调整高度后宽度超出单元格if shape.Width > target_cell.Width:# 改为按宽度调整shape.Width = target_cell.Width# 对齐到目标单元格,稍微加一点偏移(必须要加,否则图片会超出单元格一点点)shape.Top = target_cell.Top+10shape.Left = target_cell.Left+10print(f"Adjusted cells: {adjusted_cells_list}")#不保持图片的纵横比,将图片填充单元格
def fit_image_to_cell(excel_instance):# 获取底层的COM对象workbook = excel_instance.workbookws = workbook.ActiveSheetfor shape in ws.Shapes:if shape.Type == 13: # 13对应msoPicturecurrent_cell = shape.TopLeftCell# 不锁定纵横比(设置为False)shape.LockAspectRatio = 0 # 0对应msoFalse# 调整图片大小完全填充单元格shape.Top = current_cell.Top+10shape.Left = current_cell.Left+10shape.Height = current_cell.Height-20shape.Width = current_cell.Width-20#获取所有包含图片的单元格位置列表
def extract_image_positions(excel_instance):position_list=[]# 获取底层的COM对象workbook = excel_instance.workbookws = workbook.ActiveSheetfor shape in ws.Shapes:if shape.Type == 13: # 13对应msoPicture# 获取当前所在单元格(基于左上角)current_cell = shape.TopLeftCell# 去除地址中的$符号clean_address = current_cell.Address.replace("$", "")position_list.append(clean_address)return position_list
结果: