【问题标题】:Openpyxl update data of a file instead of creating a newOpenpyxl更新文件的数据而不是创建一个新的
【发布时间】:2020-11-13 18:21:49
【问题描述】:

我正在构建一个小工具来检查网站上的超链接是否会消失或保留。 该程序到目前为止工作正常,但总是创建一个新文件并删除一天前创建的最新工作表(即每天的脚本)。

我的代码如下

    def save_results(excel_urls,erg):
        currentdate = str(date.today())
        excel_file = openpyxl.load_workbook('Links2.xlsx')
        excel_sheet = excel_file['Sheet']
        book = Workbook()
        sheet = book.active
        #this following code is from my main sheet to keep it in the following file as well. its also not a proper update function but works so far. 
        sheet.column_dimensions['A'].width = 60
        sheet.column_dimensions['B'].width = 60
        for row in excel_sheet.values:
            sheet.append(row)
        #Here im going to create the other sheets with the results of my crawl
        book.create_sheet(currentdate)
        book[currentdate].column_dimensions['A'].width = 60
        book[currentdate].column_dimensions['B'].width = 60
        for keys,values in enumerate(excel_urls):
            y = values[0], str(erg[keys])
            book[currentdate].append(y)
        book.save('Links2.xlsx')

我找不到只更新而不覆盖现有文件的方法。在保护结果之前包含读取的所有内容的功能也可以正常工作。

提前感谢您的帮助!

我的功能并没有真正起作用。

    excel_file = openpyxl.load_workbook('Links2.xlsx')
    book = Workbook()
    for z,i in enumerate(excel_file.sheetnames):
        book.create_sheet('{}'.format(i))
        print(excel_file.sheetnames[z])
        for row in excel_file[excel_file.sheetnames[i]].values:
            #print(row)
            excel_file[excel_file.sheetnames[i]].append(row)
    book.save('Links2.xlsx')```

【问题讨论】:

  • 更新和覆盖没有区别。
  • 谢谢,我早就料到了。所以我尝试编写一个将所有现有数据附加到文件的函数,但它不起作用。将在我的帖子中添加代码。

标签: python-3.x beautifulsoup web-crawler openpyxl


【解决方案1】:
def save_exisiting_data():      
    excel_file = openpyxl.load_workbook('Links2.xlsx')
    book = Workbook(write_only=True)
    for z, i in enumerate(excel_file.sheetnames):
        book.create_sheet('{}'.format(i))
        book[i].column_dimensions['A'].width = 60
        book[i].column_dimensions['B'].width = 60
        for row in excel_file[excel_file.sheetnames[z]].values:
            book[excel_file.sheetnames[z]].append(row)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 2017-01-28
    • 2013-03-23
    • 1970-01-01
    相关资源
    最近更新 更多