Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重命名Excel选项卡_Python_Python 3.x_Openpyxl - Fatal编程技术网

Python 重命名Excel选项卡

Python 重命名Excel选项卡,python,python-3.x,openpyxl,Python,Python 3.x,Openpyxl,我正在尝试重命名excel电子表格中的选项卡,但它每次都会为其中一个选项卡生成此名称:“of Behavior Health Reps 51”不知道为什么。每个文件中只有一个匹配的选项卡。它也不会对任何其他选项卡执行此操作 text_search = pd.DataFrame( {'text_to_search': [ 'Retro' ,'Prior' ,'Concurr' ,'Rate' ,'claims pd' ,'health reps' ,'External'] , 'Rep

我正在尝试重命名excel电子表格中的选项卡,但它每次都会为其中一个选项卡生成此名称:“of Behavior Health Reps 51”不知道为什么。每个文件中只有一个匹配的选项卡。它也不会对任何其他选项卡执行此操作

text_search =  pd.DataFrame(
{'text_to_search':
    [
'Retro'
,'Prior'
,'Concurr'
,'Rate'
,'claims pd'
,'health reps'
,'External']
, 
'Replace':
[
'UR - Retrospective (1)'
,'UR - Prior Auth Req (2)'
,'UR - Concurrent Auth Req (2)'
,'Rate of First Level Appeals (3)'
,'Pct of Claims Paid (4)'
,'# of Behavioral Health Reps (5)' 
,'External Appeals (9)']})


for mco in files:
    wb = xl.load_workbook(mco, data_only=True)
    for sheet in wb.sheetnames:
        for index, row in text_search[0:].iterrows():           
            #print(row['text_to_search'],row['Replace'])
            if re.search(row['text_to_search'], sheet, re.IGNORECASE):
                worksheet = wb[sheet]
                worksheet.title = row['Replace']
    wb.save(mco)
    wb.close()``` 

如果它看起来像映射,则使用映射:

l1 = [
'Retro'
,'Prior'
,'Concurr'
,'Rate'
,'claims pd'
,'health reps'
,'External']

l2 =[
'UR - Retrospective (1)'
,'UR - Prior Auth Req (2)'
,'UR - Concurrent Auth Req (2)'
,'Rate of First Level Appeals (3)'
,'Pct of Claims Paid (4)'
,'# of Behavioral Health Reps (5)' 
,'External Appeals (9)']

mapping = {k:v for k,v in zip(l1, l2)}

for ws in wb:
     if s.title in mapping:
          s.title = mapping[s.title]
如果可能出现大小写问题,请转换为小写:

l1 = [v.lower() for v in l1]
if s.title.lower() in mapping:

如果它看起来像映射,则使用映射:

l1 = [
'Retro'
,'Prior'
,'Concurr'
,'Rate'
,'claims pd'
,'health reps'
,'External']

l2 =[
'UR - Retrospective (1)'
,'UR - Prior Auth Req (2)'
,'UR - Concurrent Auth Req (2)'
,'Rate of First Level Appeals (3)'
,'Pct of Claims Paid (4)'
,'# of Behavioral Health Reps (5)' 
,'External Appeals (9)']

mapping = {k:v for k,v in zip(l1, l2)}

for ws in wb:
     if s.title in mapping:
          s.title = mapping[s.title]
如果可能出现大小写问题,请转换为小写:

l1 = [v.lower() for v in l1]
if s.title.lower() in mapping:

你到底为什么要使用datafame和regex?这段代码看起来不必要的复杂和难以调试。如果你简化它,它将更容易测试。@charlectlark你真的是我的敌人你真的不需要这么说。你到底为什么要使用datafame和regex?这段代码看起来不必要的复杂和难以调试。如果你简化它,它将更容易测试。@charlectorlak你真的是我的敌人你真的不需要这么说。