#Python#Excel#图片

工作以来,测试繁多,测试后图片的机械性插入更是让我痛不欲生。正好之前有一些python编程经验,便自我摸索着写了个程序,通过索引数字的方式将图片一一插入Excel。效果甚好,工作效率明显提升。此处将代码贴出,大家一起学习。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os,sys
import win32com.client
import win32process
import win32api
import win32con
from time import sleepSheetNumber = 1
PictureNewName = []
DataBuffer = []
RowNumber = 1
ColumNumber = 1
TargetColor = 0
TempColor = 0
PicturePositionLeft = 0
PicturePositionTop = 0
PictureHeight = 0
PictureWidth = 0             def FindExcelName(file_dir,Suffix):                L=[]   for root, dirs, files in os.walk(file_dir):  for file in files:  if os.path.splitext(file)[1] == Suffix:L.append(os.path.join(root, file))  return Lprint("请注意:操作时请关闭其他Excel,否则该程序在运行时会自动关闭其他文件。")
print("WPS不受该程序影响")
FileRoot = os.getcwd()
print(FileRoot)FileName = FindExcelName(FileRoot,'.xls') or  FindExcelName(FileRoot,'.xlsx')
print(str(FileName[0]))
HugBuffer = input("输入Excel最大搜索范围(默认50,按Enter键跳过):") or 50
TargetColorRow,TargetColorColum = input("在Sheet1中指定位置(行,列),用于图片的插入和查询,默认(1,10),按Enter键跳过。例:1,1:") or 1,10
PictureRoot = FileRoot + '\picture'
PictureFileName = FindExcelName(PictureRoot,'.png')for PictureNumber in range(len(PictureFileName)):newstr = PictureFileName[PictureNumber].replace(PictureRoot,'')newstr1 = newstr.replace('\\tek','')newstr2 = newstr1.replace('.png','')PictureNewName.append(int(newstr2))
print('已获取picture文件夹中所有图片名')xlApp = win32com.client.Dispatch('Excel.Application')
xlBook = xlApp.Workbooks.Open(FileName[0])
sht = xlBook.Sheets(1)
TargetColor = sht.Cells(int(TargetColorRow), int(TargetColorColum)).Font.Color
print("sheet的总个数:%d" %(xlBook.Sheets.Count))                while SheetNumber <= xlBook.Sheets.Count:                   sht = xlBook.Sheets(SheetNumber)xlBook.RefreshAllfor RowNumber in range(int(HugBuffer)):for ColumNumber in range(int(HugBuffer)):TempBuffer = sht.Cells(RowNumber+1, ColumNumber+1).ValueTempColor = sht.Cells(RowNumber+1, ColumNumber+1).Font.Color      if TempBuffer != None and TempColor == TargetColor:for PictureNumber in range(len(PictureNewName)):try:if PictureNewName[PictureNumber] == int(TempBuffer):PicturePositionLeft = sht.Cells(RowNumber+1, ColumNumber+1).LeftPicturePositionTop = sht.Cells(RowNumber+1, ColumNumber+1).TopPictureHeight = sht.Cells(RowNumber+1, ColumNumber+1).Height - 2PictureWidth  = sht.Cells(RowNumber+1, ColumNumber+1).Width - 2sht.Shapes.AddPicture(PictureFileName[PictureNumber],1,1,PicturePositionLeft,PicturePositionTop,PictureWidth,PictureHeight)except:print('中文已跳过')print("已完成 [%s]" %(xlBook.Sheets(SheetNumber).Name))SheetNumber = SheetNumber + 1print("图片插入已完成,即将关闭程序")
sleep(2)xlBook.Save()
xlBook.Close()

目前程序还有一点小BUG,文件保存有时会出现问题,后面慢慢解决。

Python操作Excel自动插入图片相关推荐

  1. Python操作excel进行插入、删除行操作实例演示,利用xlwings库实现

    Python 操作 Excel 之插入.删除行 第一章:整行插入与删除 ① Excel 的插入行实例演示 ② Excel 的删除行实例演示 第一章:整行插入与删除 ① Excel 的插入行实例演示 插 ...

  2. python下载图片插入excel_Python向Excel中插入图片的简单实现方法

    本文实例讲述了Python向Excel中插入图片的简单实现方法.分享给大家供大家参考,具体如下: 使用Python向Excel文件中插入图片,这个功能之前学习xlwt的时候通过xlwt模块实现过.那时 ...

  3. Python 操作 excel 插入图片、表格、设置单元格颜色、字体(三)

    Python 操作 excel 插入图片.表格.设置单元格颜色.字体 python 操作 excel 插入图片.表格.设置单元格颜色.字体 提示:以下是本篇文章正文内容,下面案例可供参考 一.open ...

  4. ecplise插入图片太大_【Excel里插入图片时很大怎么办?我想把他设置成自动适应表格的大小。】excel图片固定大小...

    excel插入自适应单元格大小图片 excel入的图片可以设置自适应单元格大小而变化,具体操作如下: 一.使具:excel2010 二.操作步骤: 1.进入excel2010,点击"插入&q ...

  5. Python操作Excel文件:插入一列数据

    问题描述:已有Excel文件,其中包含5列数据,要求在第3列前插入一列数据,保存为新文件. 参考代码: 运行结果: --------董付国老师Python系列教材-------- 1)<Pyth ...

  6. 【在工程Word文档的大量表格中自动插入图片的python程序】

    在工程Word文档的大量表格中自动插入图片的python程序 一.问题描述 在word的表格中插入图片代码 图形界面GUI设计 程序打包 一.问题描述 我的姐姐是一个工程技术公司的资料员,经常需要在w ...

  7. Python操作Excel、Word、PPT、PDF、复杂文件、通信软件(微信、邮件、飞书、钉钉)、图片集合大全

    1.在CSDN的这几年,我写了本书! 很多朋友为了学习如何用Python自动化地处理各种文档,应该都下载过这个文档:python操作excel.word.pdf.pdf. 时至今日,仅仅在CSDN一个 ...

  8. 全网最全 Python 操作 Excel 教程,建议收藏!

    [欢迎关注微信公众号:厦门微思网络] 微思网络(官网):https://www.xmws.cn/ 0 Python Excel库对比 我们先来看一下python中能操作Excel的库对比(一共九个库) ...

  9. vb.net读取excel并写入dgv_如何用Python读取Excel中的图片?然后写入图片?

    大家好,在使用Python进行办公自动化操作时,一定少不了与Excel表格的交互,我们通常是用pandas处理表格数据,但大多数情况下,都是读取表格中的数值进行分析. 那么你知道如何使用Python读 ...

最新文章

  1. C#与RSS亲密接触
  2. c++max函数怎么用_比函数还强大的Excel分列技巧,你知道怎么用吗
  3. 解读:一种来自Facebook团队的大规模时间序列预测算法(附github链接)
  4. 从0开始搭建一个战棋游戏的AI(初级教程)
  5. crontab/ntpdate——时间同步
  6. Kubernetes-Label
  7. HTTP协议实体的基本讲解
  8. vba转换为vbs的方法_vba代码改成vbs,该怎么解决(4)
  9. 数据结构之查找算法:基本概念
  10. MySQL回滚日志(undo log)总结
  11. Excel 函数公式大全
  12. 自动驾驶领域常见专业英文名词及其含义
  13. 【USB】USB的VID和PID
  14. background-color:transparent
  15. 冒泡排序(Bubble Sort)含gif动图
  16. 考公 | 粉笔网课笔记——数量 刘凯
  17. Queen Collisions(分行列模拟)
  18. 部分opencv中的GPU加速函数(中文翻译)
  19. Mysql 常用 时间函数
  20. 动手学EDA-建模调参

热门文章

  1. php正则匹配preg_match,php正则表达式中preg_match函数的详解
  2. CUBEMX STM32F105RB U盘读写详细教程
  3. leetcode(力扣) 347. 前 K 个高频元素(优先队列 堆 哈希计数器)
  4. 又到年中,固定资产管理该何去何从?
  5. 使用cv2.VideoCapture()函数捕获笔记本内置摄像头的拍摄画面
  6. CMD(windows)操作命令大全
  7. 【音视频相关】nvidia-smi 命令扩展及问题分析举例(dmon/pmon/GPU性能相关)
  8. Activity onDestroy方法未能及时执行原因
  9. Linux 删除指定目录下指定后缀名的所有文件
  10. 使用国产KT148A语音芯片sop8封装,用户可以自己更换声音,低成本,高秒数