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
0
投稿
猜你喜欢
浅析python函数式编程
2022-07-14 22:39:16
Python+Selenium+phantomjs实现网页模拟登录和截图功能(windows环境)
2023-11-17 11:00:43
利用hasOwnProperty给数组去重的面试题分享
2023-08-06 20:48:37
python特性语法之遍历、公共方法、引用
2023-05-17 16:21:29
如何在pycharm中快捷安装pip命令(如pygame)
2023-02-26 14:53:25
详解python 拆包可迭代数据如tuple, list
2022-01-08 19:28:43
mysql设置更改root密码、mysql服务器的连接、mysql常用命令的图解
2024-01-22 15:04:13
解决python将xml格式文件转换成txt文件的问题(xml.etree方法)
2021-10-21 02:51:13
"模板化"——限制还是激发
2009-03-26 11:36:00
python编码问题汇总
2023-10-02 08:05:54
解析mysql 缓存如何使用内存
2024-01-19 05:21:32
Python 解决logging功能使用过程中遇到的一个问题
2023-05-25 11:31:05
Django中modelform组件实例用法总结
2023-09-28 14:35:49
GoLang中的互斥锁Mutex和读写锁RWMutex使用教程
2024-05-25 15:13:05
js实现限定范围拖拽的示例
2024-04-29 13:38:55
python 爬取英雄联盟皮肤并下载的示例
2023-07-22 09:57:45
在sql中实现取一行最大值或者最小值
2024-01-24 21:34:28
网页设计求职全攻略
2008-07-09 18:56:00
thinkPHP删除前弹出确认框的简单实现方法
2024-06-07 15:28:58
python读写ini文件示例(python读写文件)
2023-06-21 03:10:14