对worklog-worklogTask表的日期进行计算
from asyncio.windows_events import NULL
import xlrd
from datetime import datetime
workbook = xlrd.open_workbook(filename=r'worklog.xls')
table = workbook.sheet_by_name(sheet_name='worklogTask')
rows = table.nrows
cols = table.row_len(0)
table_list = table.row_values(rowx=0, start_colx=0, end_colx=None)
table_list = table.row_values(rowx=0, start_colx=0, end_colx=None)
# 写数据
table.put_cell(0,len(table_list),1,"作业时间",0)
table.put_cell(0,len(table_list)+1,1,"异常时间",0)
# 读取数据
diff_seconds=0
diff_abnormal_seconds=0
for row in range(rows-1):
value_start = table.cell_value(row+1, 7)
value_end = table.cell_value(row+1, 8)
value_abnormal_start = table.cell_value(row+1, 9)
value_abnormal_end = table.cell_value(row+1, 10)
if (value_start!='') and (value_end!=''):
value_start=value_start[:10]+' '+value_start[11:19] #2022-05-12T16:15:55+08:00
value_end=value_end[:10]+' '+value_end[11:19] #2022-05-12T16:15:55+08:00
value_start_datetime_object = datetime.strptime(value_start, '%Y-%m-%d %H:%M:%S')
value_end_datetime_object = datetime.strptime(value_end, '%Y-%m-%d %H:%M:%S')
diff_seconds=(value_end_datetime_object-value_start_datetime_object).seconds
else:
diff_seconds=0
table.put_cell(row+1,len(table_list),2,diff_seconds,0)
if (value_abnormal_start!='') and (value_abnormal_end!=''):
value_abnormal_start=value_abnormal_start[:10]+' '+value_abnormal_start[11:19] #2022-05-12T16:15:55+08:00
value_abnormal_end=value_abnormal_end[:10]+' '+value_abnormal_end[11:19] #2022-05-12T16:15:55+08:00
value_start_datetime_object = datetime.strptime(value_abnormal_start, '%Y-%m-%d %H:%M:%S')
value_end_datetime_object = datetime.strptime(value_abnormal_end, '%Y-%m-%d %H:%M:%S')
diff_abnormal_seconds=(value_end_datetime_object-value_start_datetime_object).seconds
else:
diff_abnormal_seconds=0
table.put_cell(row+1,len(table_list)+1,2,diff_abnormal_seconds,0)
import xlwt
# 设置excel单元格风格
def body_style(pattern=None):
style = xlwt.XFStyle() # Create Style
# 设置字体样式
font = xlwt.Font() # Create Font
font.name = "SimSun" # 宋体
font.height = 20 * 12 # 字体大小
# 设置单元格样式
style.alignment.vert = style.alignment.VERT_BOTTOM # 垂直居中
style.alignment.horz = style.alignment.HORZ_CENTER # 水平居中
style.alignment.wrap = style.alignment.WRAP_AT_RIGHT # 自动换行
# 设置单元格背景
if pattern:
pattern = xlwt.Pattern() # Create Pattern
pattern.pattern = pattern.SOLID_PATTERN # 设置背景颜色
pattern.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...
pattern.pattern_back_colour = 4
style.pattern = pattern
# 设置边框样式
borders = xlwt.Borders() # Pattern Borders
borders.right = borders.NO_LINE # 默认没有边框,NO_LINE
borders.top = borders.THIN # 薄边框
borders.bottom = borders.MEDIUM # 虚线边框
borders.left = borders.THICK # 厚边框
borders.left_colour = 0x90 # 边框上色
borders.right_colour = 0x90
borders.top_colour = 0x90
borders.bottom_colour = 0x90
style.font = font # 样式赋值
style.borders = borders
return style
#创建excel工作薄
myWorkbook=xlwt.Workbook()
#添加Excel工作表
mySheet=myWorkbook.add_sheet("worklogTask2")
w_nrows = table.nrows
w_ncols = table.ncols
cell_styles = body_style()
myStyle=xlwt.easyxf('font: name Times New Roman, color-index red, bold on', num_format_str='#,##0.00') #数据格式
for row in range(0,w_nrows):
for col in range(0,w_ncols):
if (col==w_ncols-1) or (col==w_ncols-2):
mySheet.write(row, col, table.cell_value(row,col),style=myStyle) # 从内存中获取成绩并写入wSheet中
else:
mySheet.write(row, col, table.cell_value(row,col)) # 从内存中获取成绩并写入wSheet中
myWorkbook.save("worklog1.xls")
天道酬勤 循序渐进 技压群雄