[d]用python如何根据sheet名字、单元格名字查找?

crestshuo 2012-07-26 10:10:42
如题,用的是win32com

--------------------------
Double行动:
原帖分数:20
帖子加分:20
...全文
536 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gloveing 2012-07-31
  • 打赏
  • 举报
回复
#========================================================
# 2012/07/31 23:18:22
#========================================================
from win32com.client import Dispatch
import time

api = Dispatch('Excel.Application')
work_book = api.Workbooks.Open(r'C:\Users\ago\Desktop\test.xls')

try:

work_sheet = work_book.Worksheets


for i in range(1,27):
for j in range(1,27):
ch = 65 + j -1
ch = chr(ch) + str(i)
work_sheet(1).Cells(i,j).Value = ch

work_book.Save()

m_range = work_sheet(1).Range('A1:Z26').Find('B15')
for i in m_range:
print i

work_book.Close()

except Exception,e:
print e
work_book.Save()
work_book.Close()
Gloveing 2012-07-31
  • 打赏
  • 举报
回复
第一列 第二列
aa bb
cc dd
========================
以上这个表,要查找到cc的具体位置:
from win32com.client import Dispatch

excel = Dispatch('Excel.Application')

book = excel.Workbooks.open(r'C:\Users\ago\Desktop\1.xls')

sheet = excel.Worksheets("Sheet1")
sheet.Cells(1, 1).Font.Size = 20

cells = sheet.Cells()

num = 0

for cell in cells:
#print repr(cell)
#'''
cell = list(cell)
if 'cc' in cell:
print u'第%d行 第%d列' %( num , cell.index('cc'))
#'''
num += 1

book.Save()
book.Close()
raw_input('over')

=========================================
输出:(注:第一行记为 第0行,列也如此)
第2行 第0列
over

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧