Python win32com 操作Exce的l简单方法(必看)


Posted in Python onMay 25, 2017

实例如下:

from win32com.client import Dispatch  
import win32com.client  
class easyExcel:  
   """A utility to make it easier to get at Excel.  Remembering 
   to save the data is your problem, as is  error handling. 
   Operates on one workbook at a time."""  
   def __init__(self, filename=None): #打开文件或者新建文件(如果不存在的话) 
     self.xlApp = win32com.client.Dispatch('Excel.Application')  
     if filename:  
       self.filename = filename  
       self.xlBook = self.xlApp.Workbooks.Open(filename)  
     else:  
       self.xlBook = self.xlApp.Workbooks.Add()  
       self.filename = '' 
    
   def save(self, newfilename=None): #保存文件 
     if newfilename:  
       self.filename = newfilename  
       self.xlBook.SaveAs(newfilename)  
     else:  
       self.xlBook.Save()    
   def close(self): #关闭文件 
     self.xlBook.Close(SaveChanges=0)  
     del self.xlApp  
   def getCell(self, sheet, row, col): #获取单元格的数据 
     "Get value of one cell"  
     sht = self.xlBook.Worksheets(sheet)  
     return sht.Cells(row, col).Value  
   def setCell(self, sheet, row, col, value): #设置单元格的数据 
     "set value of one cell"  
     sht = self.xlBook.Worksheets(sheet)  
     sht.Cells(row, col).Value = value 
   def setCellformat(self, sheet, row, col): #设置单元格的数据 
     "set value of one cell"  
     sht = self.xlBook.Worksheets(sheet)  
     sht.Cells(row, col).Font.Size = 15#字体大小 
     sht.Cells(row, col).Font.Bold = True#是否黑体 
     sht.Cells(row, col).Name = "Arial"#字体类型 
     sht.Cells(row, col).Interior.ColorIndex = 3#表格背景 
     #sht.Range("A1").Borders.LineStyle = xlDouble 
     sht.Cells(row, col).BorderAround(1,4)#表格边框 
     sht.Rows(3).RowHeight = 30#行高 
     sht.Cells(row, col).HorizontalAlignment = -4131 #水平居中xlCenter 
     sht.Cells(row, col).VerticalAlignment = -4160 # 
   def deleteRow(self, sheet, row): 
     sht = self.xlBook.Worksheets(sheet) 
     sht.Rows(row).Delete()#删除行 
     sht.Columns(row).Delete()#删除列
   def getRange(self, sheet, row1, col1, row2, col2): #获得一块区域的数据,返回为一个二维元组 
     "return a 2d array (i.e. tuple of tuples)"  
     sht = self.xlBook.Worksheets(sheet) 
     return sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value  
   def addPicture(self, sheet, pictureName, Left, Top, Width, Height): #插入图片 
     "Insert a picture in sheet"  
     sht = self.xlBook.Worksheets(sheet)  
     sht.Shapes.AddPicture(pictureName, 1, 1, Left, Top, Width, Height)  
   
   def cpSheet(self, before): #复制工作表 
     "copy sheet"  
     shts = self.xlBook.Worksheets  
     shts(1).Copy(None,shts(1))

   def inserRow(self,sheet,row):
     sht = self.xlBook.Worksheets(sheet)
     sht.Rows(row).Insert(1)

   #下面是一些测试代码。
if __name__ == "__main__":  
   #PNFILE = r'c:/screenshot.bmp' 
   xls = easyExcel(r'd:\jason.li\Desktop\empty_book.xlsx')   
   #xls.addPicture('Sheet1', PNFILE, 20,20,1000,1000)  
   #xls.cpSheet('Sheet1') 
   xls.setCell('sheet1',2,'A',88) 
   row=1 
   col=1 
   print("*******beginsetCellformat********") 
   # while(row<5):
   #  while(col<5):
   #    xls.setCellformat('sheet1',row,col)
   #    col += 1
   #    print("row=%s,col=%s" %(row,col))
   #  row += 1
   #  col=1
   #  print("*******row********")
   # print("*******endsetCellformat********")
   # print("*******deleteRow********")
   # xls.deleteRow('sheet1',5)
   xls.inserRow('sheet1',7)
   xls.save()  
   xls.close()

以上这篇Python win32com 操作Exce的l简单方法(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python实现的几个常用排序算法实例
Jun 16 Python
Python编程中的异常处理教程
Aug 21 Python
用python实现简单EXCEL数据统计的实例
Jan 24 Python
Python使用三种方法实现PCA算法
Dec 12 Python
python中的文件打开与关闭操作命令介绍
Apr 26 Python
Python unittest模块用法实例分析
May 25 Python
tensorflow实现图像的裁剪和填充方法
Jul 27 Python
详解python中的模块及包导入
Aug 30 Python
python 类之间的参数传递方式
Dec 20 Python
python编写一个会算账的脚本的示例代码
Jun 02 Python
python中scrapy处理项目数据的实例分析
Nov 22 Python
如何在 Matplotlib 中更改绘图背景的实现
Nov 26 Python
python win32 简单操作方法
May 25 #Python
python爬虫入门教程--利用requests构建知乎API(三)
May 25 #Python
Python正则表达式完全指南
May 25 #Python
Tensorflow简单验证码识别应用
May 25 #Python
Python 编码Basic Auth使用方法简单实例
May 25 #Python
Python 含参构造函数实例详解
May 25 #Python
Python爬虫之模拟知乎登录的方法教程
May 25 #Python
You might like
php遍历数组的方法分享
2012/03/22 PHP
PHP substr()函数参数解释及用法讲解
2017/11/23 PHP
利用PHP内置SERVER开启web服务(本地开发使用)
2020/01/22 PHP
Javascript 网页水印(非图片水印)实现代码
2010/03/01 Javascript
Jquery解析json字符串及json数组的方法
2015/05/29 Javascript
jquery+json实现动态商品内容展示的方法
2016/01/14 Javascript
jquery点击改变class并toggle的实现代码
2016/05/15 Javascript
利用Node.js制作爬取大众点评的爬虫
2016/09/22 Javascript
JS动态给对象添加属性和值的实现方法
2016/10/21 Javascript
JavaScript 中对象的深拷贝
2016/12/04 Javascript
详解Angualr 组件间通信
2017/01/21 Javascript
jQuery+C#实现参数RSA加密传输功能【附jsencrypt.js下载】
2017/06/26 jQuery
ReactNative之键盘Keyboard的弹出与消失示例
2017/07/11 Javascript
全面解析jQuery中的$(window)与$(document)的用法区别
2017/08/15 jQuery
详解promise.then,process.nextTick, setTimeout 以及 setImmediate的执行顺序
2018/11/21 Javascript
Node.js API详解之 assert模块用法实例分析
2020/05/26 Javascript
vue开发简单上传图片功能
2020/06/30 Javascript
Vue实现简单计算器
2021/01/20 Vue.js
Vue+Bootstrap实现简易学生管理系统
2021/02/09 Vue.js
在Python中使用pngquant压缩png图片的教程
2015/04/09 Python
Python中decorator使用实例
2015/04/14 Python
Python numpy生成矩阵、串联矩阵代码分享
2017/12/04 Python
Python 限制线程的最大数量的方法(Semaphore)
2019/02/22 Python
python 魔法函数实例及解析
2019/09/25 Python
Python 私有属性和私有方法应用场景分析
2020/06/19 Python
Python同时处理多个异常的方法
2020/07/28 Python
拥有超过850家商店的美国在线派对商店:Party City
2018/10/21 全球购物
SEPHORA丝芙兰捷克官网:购买香水、化妆品和护肤品
2018/11/26 全球购物
澳大利亚家庭花园和DIY工具网店:VidaXL
2019/05/03 全球购物
西雅图电动自行车公司:Rad Power Bikes
2020/02/02 全球购物
小学教师的自我评价范例
2013/10/31 职场文书
大学生期末自我鉴定
2014/02/01 职场文书
向女朋友道歉的话
2015/01/20 职场文书
2015年度销售个人工作总结
2015/03/31 职场文书
Python中异常处理用法
2021/11/27 Python
Golang流模式之grpc的四种数据流
2022/04/13 Golang