Python 将数据附加到现有excel电子表格

Python 将数据附加到现有excel电子表格,python,excel,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)

我编写了以下函数来完成此任务

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文件。

您应该使用
xlrd.open\u 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")

@对不起,我修好了。它是
书籍
对象。名称错误:全局名称“wb”不是defined@komalbhardwaj
sheet1=book.get_sheet(0)
,检查上面的固定代码注意在复制过程中,一些数据可能会丢失。例如一些公式,这意味着在某些情况下,您无法使用xlrd/xlwt/xlutils将数据附加到现有excel。