读取大型 Excel 表格(猜测大小)

发布于 2024-12-06 16:01:24 字数 201 浏览 1 评论 0 原文

我正在使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一页 2024-12-13 16:01:24

您可以使用 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 by sheet = book.sheet_by_index(sheetx) or sheet = book.sheet_by_name(sheet_name) and you have sheet.nrows property with number of rows in given sheet.

Here is the API documentation.

猥琐帝 2024-12-13 16:01:24

在 VBA 中,我们经常使用 End 语句,如下所示:

Worksheets("sheet1").Cells(Rows.Count, "A").End(xlUp).Row

这可能会帮助您找到列中最后使用的单元格(但是,我不知道如何将此方法扩展到 pywin< /代码>)

In VBA, we often use the End statement like this:

Worksheets("sheet1").Cells(Rows.Count, "A").End(xlUp).Row

That may help you find the last used cell of a column (yet, I don't know how to extend this method to pywin)

尝蛊 2024-12-13 16:01:24

如何使用 Worksheet.UsedRange 属性?

How about getting the whole range with the Worksheet.UsedRange property ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文