使用xlrd读取Excel工作表,并在您搜索的字符串下方找到一个单元格值

时间:2018-02-28 20:42:22

标签: python excel xlrd

我是新人,请原谅我的无知,如果我问错了请。我需要搜索工作簿来查找字符串,然后我需要从该字符串正下方的单元格中获取单元格值。当我找到该值时,我想接受它并更改.xls的文件名以保留文件名的前三个字母并将找到的值附加到它上面。它可以在工作表中的任何位置,没有标题,它更像是一个表单。感谢任何帮助,我似乎无法找到答案。我试过的是找到我搜索的字符串的单元格值。我只是无法在下面的单元格中提取数据。我有数千个这样的文件需要查看和2个可能的字符串,虽然我还没有在我的python代码中得到那么远:

import os
from xlrd import open_workbook
path = "a_path"

for filename in os.listdir(path):


    book = open_workbook("some_workbook")


for sheet in book.sheets():
for rowidx in range(sheet.nrows):
    row = sheet.row(rowidx)
    for colidx, cell in enumerate(row):

        if cell.value == "String" :
                 print (sheet.name)
                 print (colidx)
                 print (rowidx)
                 print (cell.value)

新代码。我更进一步,但是我现在无法找到限制列的方法,因为我已经到了下一行。

import os
import re
import xlrd
def rename_excel_files():
path = filename

for filename in os.listdir(path):   
    try:    
        book = xlrd.open_workbook(path + filename)
    except:
        pass
    try:
        sheet = book.sheet_by_index(0)
        for rowidx in range(sheet.nrows):
            row = sheet.row(rowidx)
            for colidx, cell in enumerate(row):
                m = re.search(r' string ', str(cell.value))
                if m: 
                    nextrow = sheet.row(rowidx + 1)
                    for i, cell2 in enumerate(nextrow):
                        newfileERnbr = (str(cell2.value).split('.')[0])
                        print (filename + newfileERnbr)
    except:
        pass                   
rename_excel_files()            

1 个答案:

答案 0 :(得分:0)

这是适合我的代码。它并不完美,但效果非常好。希望将来可能对其他人有所帮助。感谢。

代码:

import os
import re
import xlrd
def rename_excel_files():
    my_list = [list] # list is your list of locations
    for path in my_list:
        for filename in os.listdir(path):   
            try:    
                book = xlrd.open_workbook(path + filename)
            except:
                pass
            try:
                sheet = book.sheet_by_index(0) #sheet number 0 based
                for rowidx in range(0,50): #row range
                    row = sheet.row(rowidx)
                    for colidx, cell in enumerate(row):
                        m = re.search(research string, str(cell.value)) #research string is you regex that you are looking for, re paramaters will change for need below
                        q = re.search(research string, str(cell.value)) #research string is you regex that you are looking for
                        if m:
                            celllocation = sheet.cell(rowidx + 1 , colidx)
                            p = (str(celllocation).split('.')[0])
                            n = re.search('\d+', p)                        
#  test                         print (path + filename + '---------->', path + filename[0:3] + '_' + n.group() + ".xls")
                            if os.path.isfile(path + filename[0:3] + '_' + n.group() + ".xls"):
                                print ("File Exists Already " + path + filename)
                            else:
                                print (path + filename + '---------->', path + filename[0:3] + '_' + n.group() + ".xls")
                                os.rename (path + filename, path + filename[0:3] + '_' + n.group() + ".xls")
                        elif q:
                            celllocation = sheet.cell(rowidx + 1 , colidx)
                            p = (str(celllocation).split('.')[0])
                            n = re.search('\d+', p)
#    test                       print (path + filename + '---------->', path + filename[0:3] + '_' + n.group() + ".xls")
                            if os.path.isfile(path + filename[0:3] + '_' + n.group() + ".xls"):
                                print ("File Exists Already " + path + filename)
                            else:
                                print (path + filename + '---------->', path + filename[0:3] + '_' + n.group() + ".xls")
                                os.rename (path + filename, path + filename[0:3] + '_' + n.group() + ".xls")
                sheet.unload () #speeds the whole thing up because you are closing the sheet and moving on to the next one
            except:
                pass                   
rename_excel_files()