python 使用win32com 操作excel

简介:

举例1

import win32com.client as win32

xl = win32.Dispatch('Excel.Application')
xl.Visible = True
xl.Workbooks.Add()

xlBook = xl.Workbooks(1)
xlSheet = xl.Sheets(1)
xlSheet.Cells(1,1).Value = 'What shall be the number of thy counting?'
xlSheet.Cells(2,1).Value = 3
print xlSheet.Cells(1,1).Value
print xlSheet.Cells(2,1).Value


举例2

#coding:utf-8
import win32com.client as win32
import time
import pythoncom

now = time.time()
print now 

time_object = pythoncom.MakeTime(now)
print int(time_object)

xl = win32.Dispatch('Excel.Application')
xl.Visible = True
xl.Workbooks.Add()

xlBook = xl.Workbooks(1)
xlSheet = xl.Sheets(1)
xlSheet.Cells(1,1).Value = 'What shall be the number of thy counting?'
xlSheet.Cells(2,1).Value = 3
#print xlSheet.Cells(2,1).Value
xlSheet.Cells(3,1).Value = time_object
#print xlSheet.Cells(3,1).Value


xlSheet.Cells(4,1).Formula = '=A2*2'
#print xlSheet.Cells(4,1).Value
#print xlSheet.Cells(4,1).Formula

xlSheet.Cells(1,1).Value = None
#print xlSheet.Cells(1,1).Value

myRange1 = xlSheet.Cells(4,1)           #一个单元格
myRange2 = xlSheet.Range("B5:C10")      #
myRange3 = xlSheet.Range(xlSheet.Cells(2,2), xlSheet.Cells(3,8))


举例3

class easyExcel:
    """A utility to make it easier to get at Excel. Remebering 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 sace(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 set(self, sheet, row, col, value):
        "Set value of one cell"
        sht = self.xlBook.Worksheets(sheet)
        sht.Cells(row, col).Value = value
    
    


      本文转自独弹古调  51CTO博客,原文链接:http://blog.51cto.com/hunkz/1891443,如需转载请自行联系原作者






相关文章
|
19天前
|
Python
python生成excel文件的三种方式
python生成excel文件的三种方式
32 1
python生成excel文件的三种方式
|
19天前
|
数据可视化 Python
我是如何把python获取到的数据写入Excel的?
我是如何把python获取到的数据写入Excel的?
31 2
|
16天前
|
前端开发 Python
使用Python+openpyxl实现导出自定义样式的Excel文件
本文介绍了如何使用Python的openpyxl库导出具有自定义样式的Excel文件,包括设置字体、对齐方式、行列宽高、边框和填充等样式,并提供了完整的示例代码和运行效果截图。
20 1
使用Python+openpyxl实现导出自定义样式的Excel文件
|
2天前
|
数据可视化 数据格式 索引
我用Python操作Excel的两种主要工具
我用Python操作Excel的两种主要工具
|
4天前
|
SQL JSON 关系型数据库
n种方式教你用python读写excel等数据文件
n种方式教你用python读写excel等数据文件
|
3天前
|
算法 数据挖掘 Java
日常工作中,Python+Pandas是否能代替Excel+VBA?
日常工作中,Python+Pandas是否能代替Excel+VBA?
|
2月前
|
存储 监控 数据处理
💻Python高手必备!文件系统操作秘籍,让你的数据存取如臂使指
【7月更文挑战第29天】在数据驱动时代, Python以简洁语法、丰富库生态和强大跨平台能力, 成为数据科学等领域首选。本文探讨Python文件系统操作秘籍, 助力高效数据处理。
35 11
|
24天前
|
Python
【Python】解决pandas读取excel,以0向前填充的数字会变成纯数字
本文介绍了两种解决Python使用pandas库读取Excel时,数字前填充的0丢失问题的方法:一是在读取时指定列以字符串格式读取,二是在Excel中预先将数值转换为文本格式。
38 0
【Python】解决pandas读取excel,以0向前填充的数字会变成纯数字
|
2月前
|
安全 数据安全/隐私保护 Python
下一篇
云函数