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

时间: 2024-12-30 00:13:14

python 使用win32com 操作excel的相关文章

Python学习笔记-操作excel

python操作excel:使用pip安装即可 一.xlwt:写excel import xlwt book = xlwt.Workbook() #新建一个excel sheet = book.add_sheet('sheet1') #加sheet页 sheet.write(0,0,'姓名') #行.列.写入的内容 sheet.write(0,1,'年龄') sheet.write(0,2,'性别') book.save('stu.xls') #结尾一定要用.xls import xlwt ti

python通过openpyxl操作excel

python 对Excel操作常用的主要有xlwt.xlrd.openpyxl ,前者xlwt主要适合于对后缀为xls比较进行写入,而openpyxl主要是针对于Excel 2007 以上版本进行操作,也就是对后缀为xlsx进行操作. Excel 主要有三大元素,工作簿,Sheet 页,单元格,一个工作簿可以包含多个Sheet页面,而Sheet页由N多个单元格组成,而单元格主要用来存储数据: 一.安装插件 pip install openpyxl 二.创建Excel文件 操作excel之前,首先

用python库openpyxl操作excel,从源excel表中提取信息复制到目标excel表中

现代生活中,我们很难不与excel表打交道,excel表有着易学易用的优点,只是当表中数据量很大,我们又需要从其他表册中复制粘贴一些数据(比如身份证号)的时候,我们会越来越倦怠,毕竟我们不是机器,没法长时间做某种重复性的枯燥操作.想象这样一个场景,我们有个几千行的表要填,需要根据姓名输入其对应的身份证号,但之前我们已经做过一个类似的表,同样的一些人的姓名跟身份证号是完整的,那么我们就需要通过一个个查找姓名,然后把身份证号码复制到我们当前要做的表里去. 当我日复一日重复着这些操作的时候,我都很想有

python+xlrd+xlwt操作excel

介绍 xlrd(读操作),xlwt(写操作) 上述软件下载后,分别解压,之后在cmd命令下分别进入对应的目录中运行 python setup.py install 如果运行过程中提示缺少setuptools,则先运行python ez_setup.py之后在重复上面的步骤 PS:office的版本不要用太高的,建议最好用03版本的,且后缀为xls的 源码bug修复 安装好xlwt3后,找到formula.py文件,将其中的 __slots__ = ["__init__", "

python使用xlrd 操作Excel读写

此文章非本人 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 二.使用介绍 1.导入模块 import xlrd 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFile.xls') 默认操作桌面上的excel 3.使用技巧 获取一个工作表 table = data.sheets()[0]          #通过索引顺序获取 table =

Python安装和操作EXCEL数据

一.windows下面安装Python 1.安装Python 选择的版本是3.5.2版本.windows下面的Python安装一般是通过软件安装包安装而不是命令行,所以首先要在Python的官方主页上面下载最新的Python安装包.下载地址是:https://www.python.org/downloads/ 下载好后,解压到文件夹中,一直点击下一步就OK了.在安装完成之后,打开控制台,输入“Python”,我们能够看到下面的效果: 因为python.exe文件在 Python 目录下,我们还没

python自动化--模块操作之re、MySQL、Excel

一.python自有模块正则 1 import re 2 3 # re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None 4 print(re.match("www","wwwwccc").group()) #在起始位置匹配 5 print(re.match("www","wcccwww")) #不在起始位置匹配,返回None 6 7 # re.search扫描整个字符串并返回第一个成

python操作excel

python操作exce的方式: 使用win32com 使用xlrd(读excel).xlwt(写excel) 1.使用win32com方式 代码: # coding=utf-8 from win32com.client import Dispatch import pywintypes ''' 查看excel最大行数和列数 打开一个空白新建EXCEL表格,按CTRL+下箭头,可以查看到最大行数:按CTRL+右箭头, 可以查看到最大列标(若想显示列数,可在最右一列的某单元格中输入=column(

Python操作excel的几种方式--xlrd、xlwt、openpyxl

openpyxl xlrd xlwt 在处理excel数据时发现了xlwt的局限性–不能写入超过65535行.256列的数据(因为它只支持Excel 2003及之前的版本,在这些版本的Excel中行数和列数有此限制),这对于实际应用还是不够的.为此经过一番寻找发现了一个支持07/10/13版本Excel的openpyxl,虽然功能很强大,但是操作起来感觉没有xlwt方便.下面分别说下几个模块的常用操作. xlrd xlrd是用来从Excel中读写数据的,但我平常只用它进行读操作,写操作会遇到些问