标签:python excel python操作excel xlwings win32com
记录一个实际的需求,现在大多数都使用xlrd\xlwt模块,不过他们的功能不能满足我的需求,所以采用了xlwings和win32com模块组合。
# -*- coding: utf-8 -*-
import xlwings as xw
from win32com.client import Dispatch
import re
xl = Dispatch("Excel.Application")
xl.Visible = True
wb = xl.Workbooks.Open("D:\MyConfiguration\Desktop\计算人力.xlsx")
sh = wb.Sheets("Sheet1")
wbv = xw.Book("D:\MyConfiguration\Desktop\计算人力.xlsx")
def printvalue(test,i):
if ‘人力‘ in test:
a = test.split(‘人力‘)[1:][0]
if a ==‘‘:
b = test.split(‘人力‘)[0]
if ‘.‘ in b[-2:]:
return b[-3:]
elif ‘.‘ in b[-3:]:
return b[-4:]
else:
return re.findall(r"\d+\.?\d*",a)[0]
elif ‘.‘ in test[-2:]:
return test[-3:]
elif ‘.‘ in test[-3:]:
return test[-4:]
elif (sh.Cells(i,2).Value==‘休息‘):
return 0
else:
if ‘:‘ in test:
returnlist = [‘woring‘,test.split(‘:‘)[1:][0]]
return returnlist
else:
returnlist = [‘woring‘,test]
return returnlist
for i in range(2,100):
try:
comment = sh.Cells(i,2).Comment.Text()
valuetype = r"<class ‘list‘>"
if str(type(printvalue(comment,i)))!=valuetype:
xw.Range(‘c‘+str(i)).value = printvalue(comment,i).strip()
else:
xw.Range(‘c‘+str(i)).color = (255,0,0)
xw.Range(‘c‘+str(i)).value = printvalue(comment,i)[1].strip()
except Exception as e:
if (sh.Cells(i,2).Value==‘休息‘):
xw.Range(‘c‘+str(i)).value = ‘0‘
continue
elif (sh.Cells(i,1).Value==None and sh.Cells(i,2).Value==None):
break
elif (sh.Cells(i,2).Value==None):
xw.Range(‘c‘+str(i)).value = ‘0‘
continue
else:
xw.Range(‘c‘+str(i)).value = ‘1‘
continue
wbv.save()读取excel标注里的内容,写在后面的单元格内,并且对内容进行一定的过滤
本文出自 “我的博客” 博客,请务必保留此出处http://10552290.blog.51cto.com/10542290/1882777
标签:python excel python操作excel xlwings win32com
原文地址:http://10552290.blog.51cto.com/10542290/1882777