-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop_excel.py
67 lines (64 loc) · 1.8 KB
/
op_excel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# !/usr/bin/env python
# -*- coding:utf-8 -*-
"""
可以提前做好一个excel模版, 基于这个模版填充要写的excel数据
"""
import xlwt
import xlrd
import commands
import datetime
from xlutils.copy import copy
import sendmail_v2
import sys
reload(sys)
sys.setdefaultencoding('utf8')
def create_excel(filename):
date_filename = str(datetime.date.today())
oldwb = xlrd.open_workbook('tmp_gx.xls', formatting_info=True);
newwb = copy(oldwb);
news = newwb.get_sheet(0);
file = open(filename, 'r')
content = file.readlines()
ncontent = []
#print content
row = 2
for i in range(len(content)):
tmp = content[i].split('$$')
ip = tmp[0]
port = tmp[1]
add_red = tmp[2].lower()
in_out = tmp[3]
#中文需要转换成unicode,否则无法写入,会报错
addr = unicode(tmp[5])
bus = unicode(tmp[6])
# 写入数据
news.write(row, 0, ip)
news.write(row, 1, bus)
news.write(row, 2, addr)
if add_red == 'add':
news.write(row, 3, port)
elif add_red == 'red':
news.write(row, 4, port)
else:
pass
news.write(row, 7, in_out)
row += 1
if in_out.lower() != 'out':
ncontent.append(content[i])
newwb.save(date_filename)
content = '\n'.join(ncontent)
return(content, date_filename)
status, output = commands.getstatusoutput('wc -l guanxing_result.txt')
line = output.split()
line = int(line[0])
if line > 0:
content, filename = create_excel('guanxing_result.txt')
mail_subject = u'观星台审计'
for i in range(10):
result = sendmail_v2.get_conf(mail_subject, content, filename)
if result == 0:
break
else:
continue
else:
print 'nothing!'