简体   繁体   English

在Excel中复制数据-openpyxl

[英]Copying data in excel - openpyxl

I use the code fragment below to copy data to an excel file using openpyxl. 我使用下面的代码片段使用openpyxl将数据复制到excel文件中。

      for i, j in enumerate(fint):
       _= ws1.cell(column=i +1, row=globdat.cycle, value=j)

   wb.save(filename = dest_filename)

BUT

after doing so I have found the code to become very slow. 这样做之后,我发现代码变得非常慢。 is there a better way of doing this ? 有更好的方法吗?

globdat.cycle is a count for each iteration. globdat.cycle是每次迭代的计数。

'fint' is a row vector with an unknown number of entries in each iteration. 'fint'是一个行向量,每次迭代中的条目数未知。

Nb- I am working with large amounts of data so only openpyxl seem to work. Nb-我正在处理大量数据,因此似乎只有openpyxl有效。

Unfortunately, due to the way data is stored in Excel it's quite difficult to do copy operations. 不幸的是,由于数据在Excel中的存储方式,很难进行复制操作。 The xlutils library does include some functions designed specifically for this but I don't know if they work for xlsx files. xlutils库确实包含一些专门为此设计的功能,但是我不知道它们是否适用于xlsx文件。

If you just want part of the data then I would suggest that you open the file to copy from in read-only mode and write to another one in write-only mode. 如果只需要部分数据,则建议您以只读模式打开文件进行复制,然后以只写模式写入另一个文件。 This will keep memory use down and should be pretty fast. 这样可以减少内存使用量,并且应该很快。

For other situations there were some suggestions made on the bug tracker: https://bitbucket.org/openpyxl/openpyxl/issue/171/ 对于其他情况,在错误跟踪器上提出了一些建议: https : //bitbucket.org/openpyxl/openpyxl/issue/171/

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

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