日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 使用win32com 操作excel

發布時間:2025/5/22 python 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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
?? ?
?? ?
?? ?
?? ?
???



轉載于:https://blog.51cto.com/hunkz/1891443

總結

以上是生活随笔為你收集整理的python 使用win32com 操作excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。