将数据追加到现有的Excel电子表格 [英] Append data to an existing excel spreadsheet

查看:105
本文介绍了将数据追加到现有的Excel电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote the following function to accomplish this task.

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文件中.我想覆盖每个网址的文件,然后将其附加到excel文件中.

For every url taken from a given .txt file, I want to be able to count the number of tags and print that to each excel file. I want to overwrite the file for each url, and then append to the excel file.

推荐答案

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

You should use xlrd.open_workbook() for loading the existing Excel file, create a writeable copy using xlutils.copy, then do all the changes and save it as.

类似的东西:

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

这篇关于将数据追加到现有的Excel电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆