iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python 使用win32com 操
  • 234
分享到

python 使用win32com 操

pythonwin32com 2023-01-31 02:01:22 234人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

举例1import win32com.client as win32xl = win32.Dispatch('excel.Application')xl.Visible = Truexl.Workbooks.Add()xlBook = xl

举例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
    
    
    
    
   



--结束END--

本文标题: python 使用win32com 操

本文链接: https://www.lsjlt.com/news/186471.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作