2015-02-06 127 views
1

我编写了以下函数来完成此任务。将数据附加到现有的Excel电子表格

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") 

为了从给定的.txt文件所采取的每个URL,我希望能够算的标签数量并打印到每个Excel文件。我想覆盖每个url的文件,然后追加到excel文件。

回答

3

您应该使用xlrd.open_workbook()加载现有的Excel文件,使用xlutils.copy创建可写副本,然后执行所有更改并将其另存为。

类似的东西:

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

@komalbhardwaj对不起,我固定它。这是“书”的对象。 – 2015-02-06 10:50:33

+0

NameError:未定义全局名称'wb' – 2015-02-06 10:54:30

+0

@komalbhardwaj'sheet1 = book.get_sheet(0)',检查上面的固定代码 – 2015-02-06 11:01:28

相关问题