【问题标题】:read an excel sheet with xlrd and find a cell value below the string you search for使用 xlrd 读取 excel 表并在您搜索的字符串下方找到一个单元格值
【发布时间】:2018-08-08 20:12:57
【问题描述】:

我是新手,如果我问错了,请原谅我的无知。我需要搜索工作簿以找到一个字符串,然后我需要从该字符串正下方的单元格中获取我的单元格值。当我找到该值时,我想获取它并更改 .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()            

【问题讨论】:

    标签: python excel xlrd


    【解决方案1】:

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

    代码:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-17
      • 2018-04-22
      • 2020-03-18
      • 1970-01-01
      • 2021-09-12
      • 2021-02-11
      • 2014-12-21
      • 1970-01-01
      相关资源
      最近更新 更多