본문 바로가기
Engineering WIKI/Python

[Python] 엑셀 코드(xlsx 파일 만들기)

by wonos 2018. 10. 15.
import xlsxwriter
 
 
book = xlsxwriter.Workbook("2-1.xlsx")    # 엑셀 파일 이름 지정
sheet1 = book.add_worksheet()           # Sheet 만들기
 
 
 
file_open = open("2-1.txt","r")            # txt 파일 읽기
 
cnt = 0                                 # row는 0
 
for lines in file_open:
    line = lines.split("\t")                # 탭으로 자르기
 
    if cnt > 1048576:                    # Row 수가 1048576을 넘으면Sheet1 추가
        sheet1 = book.add_worksheet()
 
    if str(line[0]).strip() !="":                # if line[0].strip이 공백이 아니면
        sheet1.write(cnt,0,str(line[0]).strip())    #sheet1.write(row, col, item)
 
    try :
        if str(line[1]).strip() !="":               # if line[1].strip이 공백이 아니면
            sheet1.write(cnt,1,str(line[1]).strip())    # sheet1.write(row, col, item)
    except:
        pass
    cnt +=1                                    # row수 증가
 
file_open.close()
book.close()