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)
        sheet1.write(rowx,colx+1, count)


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

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

最佳答案

您应该使用 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")

关于python - 将数据附加到现有的 Excel 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28363562/

相关文章:

java.exe 以非零退出值 1 结束

python - pexpect.run() 在结束 ffmpeg 之前终止而没有完成转换

python - 我如何线性搜索和比较两个 .text 文件以查看它们之间缺少什么?

python - 在 Python 中产生 2 次方的按位运算

arrays - 仅限 VBA - 是否有更简单的方法可以根据位置连接数组值?

excel - 导出 Excel 内部服务

json - 如何将带有 T 的 JSON 时间戳更改为 Excel 中的正常日期和时间

python - python中的主线程什么时候退出

python - 直接在图表上可视化 matplotlib 直方图 bin 计数

excel - 是什么导致整个 ListBox 列为空?