如何实现类似VBA的功能”范围。找到“使用win32?

2024-04-20 14:01:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望使用Python中的win32com包实现类似于the Range.Find method in VBA的功能。我正在处理一个Excel CSV文件。虽然我发现了很多使用range()的解决方案,但它似乎需要指定一个固定的单元格范围,而不是VBA中的Range.Find,后者将在工作表中自动搜索而不必固定范围。在

这是我的代码:

import win32com.client as client

excel= client.dynamic.Dispatch("Excel.Application")
excel.visible= True

wb= excel.workbooks.open(r"ExcelFile.xls")
ws= wb.worksheets('First')

### This able to extract information:
test_range= ws.Range("A1") 
### Got issue AttributeError: 'function' object has no attribute 'Find':
test_range= ws.Range.Find("Series ID") 
print(test_range.value)

这是否意味着Range.Find方法在win32包中不受支持,还是我用错误的现有模块指向它?在


Tags: theintest功能clientwsrangefind
3条回答

额外回答:如果你是Excel API的粉丝(10倍于@ashleedawg评论),你可以直接通过xlwings使用它:

import xlwings as xw

bookName = r'C:\somePath\hello.xlsx'
sheetName = 'Sheet1'

wb = xw.Book(bookName)
sht = wb.sheets[sheetName]

myCell = wb.sheets[sheetName].api.UsedRange.Find('test')
print('       -')
print (myCell.address)
input()

因此,输入如下:

enter image description here

很好地返回这个:

enter image description here

如果你想用一个函数来做,这是一种方法-defCell是函数的名称。在

import xlsxwriter
import os
import xlrd    
import time 
from xlsxwriter.utility import xl_rowcol_to_cell

def findCell(sh, searchedValue):
    for row in range(sh.nrows):
        for col in range(sh.ncols):
            myCell = sh.cell(row, col)
            if myCell.value == searchedValue:
                return xl_rowcol_to_cell(row, col)
    return -1

myName = 'hello.xlsx'
wbk = xlsxwriter.Workbook(myName)
wks = wbk.add_worksheet()
i = -1

for x in range(1, 1000, 11):
    i+=1
    cella = xl_rowcol_to_cell(i, 0) #0,0 is A1!
    cellb = xl_rowcol_to_cell(i, 1)
    cellc = xl_rowcol_to_cell(i, 2)
    wks.write(cella,x)
    wks.write(cellb,x*3)
    wks.write(cellc,x*4.5)
myPath= os.getcwd()+"\\"+myName

searchedValue = 300
for sh in xlrd.open_workbook(myPath).sheets():  
    print(findCell(sh, searchedValue))
input('Press ENTER to exit')

它在运行后会生成:

enter image description here

因此,在代码的第一部分,生成了一些带有随机数字的Excel文件:

import xlsxwriter
from xlsxwriter.utility import xl_rowcol_to_cell
import xlrd    

#First part of the code, used only to create some Excel file with data

wbk = xlsxwriter.Workbook('hello.xlsx')
wks = wbk.add_worksheet()
i = -1

for x in range(1, 1000, 11):
    i+=1
    cella = xl_rowcol_to_cell(i, 0) #0,0 is A1!
    cellb = xl_rowcol_to_cell(i, 1)
    cellc = xl_rowcol_to_cell(i, 2)
    #print (cella)
    wks.write(cella,x)
    wks.write(cellb,x*3)
    wks.write(cellc,x*4.5)
myPath= r'C:\Desktop\hello.xlsx'
wbk.close()

#SecondPart of the code

for sh in xlrd.open_workbook(myPath).sheets():  
    for row in range(sh.nrows):
        for col in range(sh.ncols):
            myCell = sh.cell(row, col)
            print(myCell)
            if myCell.value == 300.0:
                print('     -')
                print('Found!')
                print(xl_rowcol_to_cell(row,col))
                quit()

有了代码的第二部分,真正的“搜索”就开始了。在本例中,我们正在搜索300,它实际上是从代码的第一部分生成的值之一:

enter image description here

因此,python开始循环遍历行和列,将值与300进行比较。如果找到该值,它将写入Found并停止搜索:

enter image description here

这段代码实际上可以重写,将第二部分作为函数(def)。在

相关问题 更多 >