import os
from win32com.client import Dispatch, constants
def fun():
OBJ_PATH = os.path.dirname(os.path.realpath(__file__))
app = Dispatch('Excel.Application')
app.Visible = 1 # 0无窗口,1有窗口
app.DisplayAlerts = 0 # 0无警告提示,1有警告提示
WorkBook = app.Workbooks.Open(os.path.join(OBJ_PATH, "新建XLSX 工作表.xlsx"))
sheet = WorkBook.Worksheets('sheet1')
cell01_value = sheet.Cells(1, 1).Value
print("cell01的内容为:", cell01_value)
sheet.Cells(1, 2).Value = "win32com"
WorkBook.SaveAs(os.path.join(OBJ_PATH, "修改操作练习1.xlsx"))
WorkBook.Close()
app.Quit()
fun()
网友评论