# -*- coding: utf-8 -*-
import win32com.client
import os
#剪切板
import win32clipboard as wc
import win32con
excel = win32com.client.Dispatch('Excel.Application')
excel.DisplayAlerts = False
excel.Visible = False
myBook = excel.Workbooks.Open(r'D:\python\projects\p1\123.xlsx') #full path is must
mySheet = myBook.Sheets(1)
# mySheet.Activate #activate sheet
# mySheet.Cells(1,1).Value = 'ok123' #fill value into cells
# print(mySheet.Cells(1,1).Value) #get cells value
# print(mySheet.Name) #print sheet name
# print(mySheet.Rows.Count) #full rows count,
# print(mySheet.Columns.Count) #full columns count,
#
# myList = mySheet.Range(mySheet.Cells(1,1),mySheet.Cells(2,2)).Value # assign to a list for a range
# print(myList)
# mySheet.Range(mySheet.Cells(4,4),mySheet.Cells(5,5)).Value = myList # fill in range
# a = mySheet.Range('a1:b2').Value # get range value
# print(a)
# mySheet.Range(mySheet.Columns('A'),mySheet.Columns('C')) #从第1到3列
# print('======')
# print(myBook.Sheets.Count) # sheets count
# mySheet.Copy(Before=None,After=myBook.Sheets(2)) #copy sheet, Before=可以省略
#可以跨工作簿复制
# print(myBook.Sheets(3).Name)
# myBook.Sheets(3).Delete() #delete sheet
# b = mySheet.Range('a65536').End(-4162).Row #获取最后一行
# print(b)
# b = mySheet.Range('XFD5').End(-4159).Column #获取最后一列
# print(b)
#清除, 并不移动位置
# mySheet.Range('a1').Clear() #清除全部
# mySheet.Range('a1').ClearContents() #清除内容
# mySheet.Range('a1').ClearFormats() #清除格式
# mySheet.Range('a1').ClearComments() #清除评论
# mySheet.Range('a1:e5').Clear() #区域删除
#删除,但是会移动其他部分
# mySheet.Rows(5).Delete(Shift=-4162) #删除行,下面的行往上移动
# mySheet.Columns(5).Delete(Shift=-4159) #删除列,右边的列往左边移动
# mySheet.Columns(5).Delete() #不写shift也会默认移动
# mySheet.Cells(1,2).Formula = '=a1' #单元格输入公式
myBook.Save()
myBook.Close()
excel.Quit()
# 获取剪切板内容
def getCopy():
wc.OpenClipboard()
t = wc.GetClipboardData(win32con.CF_UNICODETEXT)
wc.CloseClipboard()
return t
# 写入剪切板内容
def setCopy(str):
wc.OpenClipboard()
wc.EmptyClipboard()
wc.SetClipboardData(win32con.CF_UNICODETEXT, str)
wc.CloseClipboard()
setCopy("中文English")
print(getCopy())