Python win32com 操作Exce的l简单方法(必看)
作者:jingxian 时间:2022-12-04 10:20:36
实例如下:
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()
标签:win32com,excel,Python
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python绘制简单折线图代码示例
2022-06-28 11:08:31
![](https://img.aspxhome.com/file/2023/4/99074_0s.jpg)
python with提前退出遇到的坑与解决方案
2023-12-24 15:02:35
Oracle9i取得建表和索引的DDL语句
2010-07-20 12:59:00
Shellcode加密解密函数
2009-04-24 11:18:00
Python中return用法案例详解
2022-09-09 10:35:40
Google的用户体验设计原则
2009-01-12 18:31:00
Pandas中Series的属性,方法,常用操作使用案例
2021-11-22 05:00:47
![](https://img.aspxhome.com/file/2023/1/83771_0s.png)
Python 3行代码提取音乐高潮部分
2021-11-14 13:15:54
![](https://img.aspxhome.com/file/2023/6/80606_0s.png)
python迭代器与生成器详解
2021-03-24 00:34:24
SQL事务用法begin tran,commit tran和rollback tran的用法
2012-01-05 18:58:51
python 2.7.13 安装配置方法图文教程
2023-11-13 11:54:37
![](https://img.aspxhome.com/file/2023/3/63953_0s.png)
另类读写:ACCESS中Field对象的标题属性
2008-11-28 16:47:00
Python绘制雷达图时遇到的坑的解决
2021-05-18 00:09:03
![](https://img.aspxhome.com/file/2023/2/99192_0s.jpg)
MySQL数据库与表的最基本命令大盘点
2010-08-31 14:29:00
Python实现数通设备端口使用情况监控实例
2021-02-09 23:56:41
banner字体设计与应用
2009-07-06 14:42:00
![](https://img.aspxhome.com/file/UploadPic/20097/6/01-44s.jpg)
利用python计算时间差(返回天数)
2023-10-22 02:39:30
python基于exchange函数发送邮件过程详解
2021-03-23 02:12:22
![](https://img.aspxhome.com/file/2023/7/96907_0s.png)
SQL Server服务器内存升级后的烦恼
2008-12-22 10:55:00
Python 如何用一行代码实现for循环初始化数组
2022-06-19 07:56:41