-
Notifications
You must be signed in to change notification settings - Fork 0
/
xlsx.py
61 lines (57 loc) · 1.69 KB
/
xlsx.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import openpyxl
def getdata_from_xlsx(xlsx):
''' return {
sheet1_name: (
(a1, b1, c1, ...), #row1
(a2, b2, c2, ...), #row2
...
),
...
}
'''
try:
wb = openpyxl.load_workbook(xlsx)
except Exception,e:
glog('error', 'can not read xlsx file: %s' % xlsx)
raise e
datas = {}
for sheet in wb.worksheets:
sheet_data = []
for row in sheet.rows:
row_value = [ cell.value for cell in row ]
sheet_data.append(row_value)
datas[sheet.title] = sheet_data
return datas
def create_xlsx(path, datas):
''' datas = {
sheet1_name: (
(a1, b1, c1, ...), #row1
(a2, b2, c2, ...), #row2
...
),
...
}
'''
from openpyxl.cell import get_column_letter
wb = openpyxl.Workbook()
first_sheet = True
for sheet in datas:
if first_sheet:
ws = wb.get_active_sheet()
first_sheet = False
else:
ws = wb.create_sheet()
ws.title = sheet
for row_index, row_data in enumerate(datas[sheet], start=1):
for col_index, row_value in enumerate(row_data, start=1):
col = get_column_letter(col_index)
if type(row_value) == type(u''):
row_value = row_value.encode('utf-8')
ws.cell('%s%s' % (col, row_index)).value = row_value
try:
wb.save(filename = path)
except Exception,e:
glog('error', 'can not save file to %s' % path)
raise e