2015-02-06 2 views
1

이 작업을 수행하기 위해 다음 함수를 작성했습니다. 주어진 .txt 파일에서 가져온 모든 URL에 대한 기존 엑셀 스프레드 시트에 데이터 추가

def write_file(url,count): 

    book = xlwt.Workbook(encoding="utf-8") 
    sheet1 = book.add_sheet("Python Sheet 1") 
    colx = 1 
    for rowx in range(1): 

     # Write the data to rox, column 
     sheet1.write(rowx,colx, url) 
     sheet1.write(rowx,colx+1, count) 


    book.save("D:\Komal\MyPrograms\python_spreadsheet.xls") 

, 나는 태그의 수를 계산하고 인쇄 할 수 있도록하려는 각 파일을 엑셀. 각 URL의 파일을 덮어 쓰고 Excel 파일에 추가하려고합니다.

답변

3

기존 Excel 파일을로드하는 데 xlrd.open_workbook()을 사용하고 xlutils.copy을 사용하여 쓰기 가능한 복사본을 만든 다음 변경 사항을 모두 저장하고 저장해야합니다. 그런

뭔가 :

@komalbhardwaj 죄송
from xlutils.copy import copy  
from xlrd import open_workbook 

book_ro = open_workbook("D:\Komal\MyPrograms\python_spreadsheet.xls") 
book = copy(book_ro) # creates a writeable copy 
sheet1 = book.get_sheet(0) # get a first sheet 

colx = 1 
for rowx in range(1): 
    # Write the data to rox, column 
    sheet1.write(rowx,colx, url) 
    sheet1.write(rowx,colx+1, count) 

book.save("D:\Komal\MyPrograms\python_spreadsheet.xls") 
+0

, 나는 그것을 해결했습니다. 그것은'book' 객체입니다. –

+0

NameError : 전역 이름 'wb'가 정의되지 않았습니다. –

+0

@komalbhardwaj 'sheet1 = book.get_sheet (0)', 위의 고정 코드를 확인하십시오. –

관련 문제