简体   繁体   English

我想让我的 excel 文件用 openpyxl 快速读写

[英]i want to make my excel file read and write fast with openpyxl

my code is to slow I want to make is more faster for example: the code is supposed to take the value of in cell a1 and change its value and rewrite again in the same cell can you help me with it?我的代码很慢,我想让它更快,例如:代码应该采用单元格 a1 中的值并更改其值并在同一个单元格中再次重写你能帮我吗?

import openpyxl

row = 1
counter = 0
while row <= 20980:
    book = openpyxl.load_workbook('semsar_full.xlsx')
    sheet = book.active
    a3 = sheet.cell(row=row, column=1)
    a4 = a3.value + ':::'
    sheet.cell(row=row, column=1, value=a4)
    counter += 1
    row += 1
    print(counter)
    book.save('semsar_full.xlsx')

I think the biggest issue while it is very slow is that you are loading + saving the file for each row contained.我认为最大的问题是虽然速度很慢,但您正在为包含的每一行加载+保存文件。 If you put the load + save outside of the loop the code should be much faster.如果将加载+保存放在循环之外,代码应该会快得多。

import openpyxl

book = openpyxl.load_workbook('semsar_full.xlsx')
sheet = book.active
row = 1
counter = 0
while row <= 20980:

    a3 = sheet.cell(row=row, column=1)
    a4 = a3.value + ':::'
    sheet.cell(row=row, column=1, value=a4)
    counter += 1
    row += 1
    print(counter)

book.save('semsar_full.xlsx')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM