将数据附加到现有的Excel电子表格

kom*_*waj 3 python 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")
Run Code Online (Sandbox Code Playgroud)

对于从给定的.txt文件中获取的每个URL,我希望能够计算标签的数量并将其打印到每个excel文件中.我想覆盖每个URL的文件,然后附加到excel文件.

Igo*_*ist 5

您应该使用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")
Run Code Online (Sandbox Code Playgroud)