Introfor

랜덤 뽑기 & excel 연동 본문

Doing/Python

랜덤 뽑기 & excel 연동

YongArtist 2017. 2. 1. 03:03

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
#-*- coding: utf-8 -*-
 
import xlrd, xlwt, random2
 
# excel 파일 값 받아오기 및 랜덤 연산
def openExcel(path):
    excel = []
    wb = xlrd.open_workbook(path)
    ws = wb.sheet_by_index(0)
    num_nrows = ws.nrows
 
    for row_num in xrange(ws.nrows):
        row = ws.row(row_num)
        cols = []
        for cell in row:
            if cell.ctype == 1:
                cols.append(cell.value)
        excel.append(','.join(cols))
 
    random2.shuffle(excel)
 
    return excel
 
# excel 파일 출력
def createExcel(excel):
 
    wb = xlwt.Workbook(encoding='utf-8')
    wb.default_style.font.height = 20*11
 
    ws = wb.add_sheet('sheet1')
 
    for i in xrange(7):
        print i
        ws.write(i,0,excel[i])
 
    wb.save('result.xls')
 
 
if __name__=='__main__':
    path = 'random.xlsx'
    excel=openExcel(path)
    createExcel(excel)
cs


'Doing > Python' 카테고리의 다른 글

enumerate 사용법  (0) 2019.07.05
How to install Pycharm on ubuntu  (0) 2017.02.02
Canvas Flower  (0) 2016.12.27
Socket_Chatting  (0) 2016.12.22
List Functions  (0) 2016.10.19
Comments