python에서 excel파일 만드는 간단한 설명 요약
xlwt -- 엑셀파일로 저장하는 모듈
- 엑셀파일 생성 간단 샘플
>>> import xlwt
>>> book = xlwt.Workbook()
>>> sheet1 = book.add_sheet('Sheet 1234')
>>> sheet1.write(0,0,'A1')
>>> sheet1.write(0,1,'B1')
>>> book.save('sample.xls')
#!/usr/bin/env python # coding: iso-8859-1 import sys,os,glob import xlwt def main(): if len(sys.argv) < 2: print "Usage: %s csvfileDir" % sys.argv[0] print "Example: %s ./out " % sys.argv[0] os.exit() fileList = glob.glob("%s/*.csv" % sys.argv[1]) fileList.sort() book = xlwt.Workbook() for file in fileList: sheetName = file[ file.find("/")+1: file.find(".") ] sheet = book.add_sheet( sheetName ) i = 0 j = 0 f = open(file) for line in f: data = line.strip().split(",") j = 0 for row in data: sheet.write( i , j, row ) j = j + 1 i = i + 1 f.close() book.save("out.xls")
- 탬플릿에 엑셀을 읽어서 내용채우는 샘플
from xlrd import open_workbook from xlwt import easyxf from xlutils.copy import copy rb = open_workbook('source.xls',formatting_info=True) rs = rb.sheet_by_index(0) wb = copy(rb) ws = wb.get_sheet(0) plain = easyxf('') for i,cell in enumerate(rs.col(2)): if not i: continue ws.write(i,2,cell.value,plain) for i,cell in enumerate(rs.col(4)): if not i: continue ws.write(i,4,cell.value-1000) wb.save('output.xls')
참고:
http://www.python-excel.org/
python-excel.pdf
댓글
댓글 쓰기