使用 win32com 和 python 的 Excel [英] Excel using win32com and python

查看:46
本文介绍了使用 win32com 和 python 的 Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用适用于 python 的 win32com 客户端在不从 Excel 工作表中进行迭代的情况下读取整列.

I wanted to know how to read an entire column without iterating from an excel sheet using win32com client for python.

推荐答案

最快的方法是通过 win32com.client API 使用内置的 Range 功能.不过,我不是它的忠实粉丝.我认为 API 令人困惑且文档不全,并且使用它不是很 Pythonic(但这只是我自己).

The fastest way would be to use the built in Range functionality through the win32com.client API. However, I'm not a big fan of it. I think the API is confusing and badly documented, and using it isn't very pythonic (but that's just me).

如果效率对您来说不是问题,您可以使用优秀的 xlrd 库.像这样:

If efficiency is not an issue for you, you can use the excellent xlrd library. Like so:

import xlrd
book = xlrd.open_workbooks('Book1')
sheet = book.sheet_by_name('Sheet1')
sheel.col(1)
sheet.col(2)
# and so on...

这为您提供了单元格对象.要获得纯值,请使用 sheet.col_values(还有其他一些非常适合使用的方法).

That gives you the cell objects. To get pure values, use sheet.col_values (and there are a few other methods that are real nice to work with).

只要记住 xlrd 代表excel read",所以如果你想写入一个 excel 文件,你需要一个名为xlwt"的不同库(这也很好,虽然在我看来不如 xlrd).

Just remember that xlrd stand for "excel read", so if you want to write to an excel file you need a different library called "xlwt" (which is also pretty good, though less so than xlrd in my opinion).

这篇关于使用 win32com 和 python 的 Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆