我正在使用 pywin32 来读取和写入 Excel。现在我知道的唯一方法是访问 Range.Value 但是,我通常不提前知道完整 Excel 表的大小。
所以此刻我逐行阅读,直到找到完全空的行。这可能会很慢。
当然,我会尝试读取数据块之类的技巧 - 然后我必须找到最佳的块大小。
您知道另一种方法(也许是一些内部 Excel 函数)或其他更快的方法吗?
I'm using pywin32 to read and write to excel. Now the only method I know is accessing Range.Value however, I usually don't know the size of the full excel table in advance.
So at the moment I read line by line until I find a completely empty line. This can be quite slow.
Of course I will try tricks like reading blocks of data - then I'd have to find an optimal block size.
Do you know another method (maybe some internal excel function) or other approaches which are faster?
发布评论
评论(3)
您可以使用 xlrd 打开工作簿并读取特定工作表的大小。速度相当快。
提示:
book = xlrd.open_workbook("myfile.xls")
然后你可以通过sheet = book.sheet_by_index(sheetx)
或sheet = book.sheet 对象获取 Sheet 对象。 sheet_by_name(sheet_name)
并且您有sheet.nrows
属性,其中包含给定工作表中的行数。这是 API 文档。
You can use xlrd to open a workbook and read the size of particular worksheet. It's quite fast.
Hints:
book = xlrd.open_workbook("myfile.xls")
then you get Sheet object bysheet = book.sheet_by_index(sheetx)
orsheet = book.sheet_by_name(sheet_name)
and you havesheet.nrows
property with number of rows in given sheet.Here is the API documentation.
在 VBA 中,我们经常使用
End
语句,如下所示:这可能会帮助您找到列中最后使用的单元格(但是,我不知道如何将此方法扩展到
pywin< /代码>)
In VBA, we often use the
End
statement like this:That may help you find the last used cell of a column (yet, I don't know how to extend this method to
pywin
)如何使用 Worksheet.UsedRange 属性?
How about getting the whole range with the Worksheet.UsedRange property ?