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使用urlparse分析网址中域名的方法
Apr 15 Python
总结Python编程中三条常用的技巧
May 11 Python
简介Django中内置的一些中间件
Jul 24 Python
Python实现桶排序与快速排序算法结合应用示例
Nov 22 Python
Python实现的knn算法示例
Jun 14 Python
python实现简单http服务器功能
Sep 17 Python
Linux下安装python3.6和第三方库的教程详解
Nov 09 Python
11个Python Pandas小技巧让你的工作更高效(附代码实例)
Apr 30 Python
20行python代码的入门级小游戏的详解
May 05 Python
pandas 中对特征进行硬编码和onehot编码的实现
Dec 20 Python
Python如何使用turtle库绘制图形
Feb 26 Python
Python OpenCV 图像平移的实现示例
Jun 04 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
phpMyAdmin2.11.6安装配置方法
2008/08/24 PHP
PHP+jquery+CSS制作头像登录窗(仿QQ登陆)
2016/10/20 PHP
PHP钩子实现方法解析
2019/05/21 PHP
Javascript控制input输入时间格式的方法
2015/01/28 Javascript
举例详解AngularJS中ngShow和ngHide的使用方法
2015/06/19 Javascript
jQuery simplePage+AJAX plus分页插件用法实例
2016/02/17 Javascript
深入理解JS实现快速排序和去重
2016/10/17 Javascript
jquery,js简单实现类似Angular.js双向绑定
2017/01/13 Javascript
Vue2路由动画效果的实现代码
2017/07/10 Javascript
Javascript快速实现浏览器系统通知
2017/08/26 Javascript
JS实现的简单表单验证功能示例
2017/10/13 Javascript
详解如何创建并发布一个 vue 组件
2018/11/08 Javascript
react写一个select组件的实现代码
2019/04/03 Javascript
p5.js码绘“跳动的小正方形”的实现代码
2019/10/22 Javascript
JS+canvas五子棋人机对战实现步骤详解
2020/06/04 Javascript
Python自动化测试ConfigParser模块读写配置文件
2016/08/15 Python
Python中顺序表的实现简单代码分享
2018/01/09 Python
Python采集代理ip并判断是否可用和定时更新的方法
2018/05/07 Python
pytorch 自定义卷积核进行卷积操作方式
2019/12/30 Python
Mac中PyCharm配置Anaconda环境的方法
2020/03/04 Python
OpenCV Python实现拼图小游戏
2020/03/23 Python
OpenCV利用python来实现图像的直方图均衡化
2020/10/21 Python
英国虚拟主机服务商:eUKhost
2016/08/16 全球购物
Spartoo葡萄牙鞋类网站:线上销售鞋履与时尚配饰
2017/01/11 全球购物
Desigual英国官网:在线购买原创服装
2018/03/09 全球购物
酒店司机岗位职责
2013/12/14 职场文书
2014全国两会心得体会
2014/03/17 职场文书
生日礼品店创业计划书范文
2014/03/21 职场文书
2014大学辅导员工作总结
2014/12/02 职场文书
检讨书范文
2015/01/27 职场文书
学校中秋节活动总结
2015/03/23 职场文书
职位证明模板
2015/06/23 职场文书
学习师德师风的心得体会(2篇)
2019/10/08 职场文书
通过T-SQL语句创建游标与实现数据库加解密功能
2022/03/16 SQL Server
MySQL限制查询和数据排序介绍
2022/03/25 MySQL
zabbix如何添加监控主机和自定义监控项
2022/08/14 Servers