简体   繁体   English

如何遍历每个Excel工作表名称

[英]how to loop through each excel sheet name

I am trying to apply conditional formatting to my workbook which has multiple sheets but the below code will not work. 我试图将条件格式应用到我的工作簿中,该工作簿有多个工作表,但是下面的代码不起作用。 I need a loop which will go through each sheet but when I run it, it doesnt recongise sheet 1. 我需要一个遍历每张纸的循环,但是当我运行它时,它不会使第一张纸变硬。

#Apply % bars to the column.
sheet_name = ['sheet1', 'sheet2', 'sheet3', 'sheet4']

for n in range(0,len(sheet_name),1):
    ws = writer.sheets[sheet_name[n]]
    ws.conditional_format('H2:H100', {'type': 'data_bar',
                                       #'bar_solid': True})

writer.save()

writer.save()放入循环中,

The OP ( debugging-mode ) script produces below print result: OP( 调试模式 )脚本产生以下打印结果:

sheet_name = ['sheet1', 'sheet2', 'sheet3', 'sheet4']

# code OP.

for n in range(0,len(sheet_name),1):

    print (n)                         # debugging to see what happens here.
    x = [sheet_name[n]]
    print (x)                         # and see what this does.

... snippet code....

And prints the following: 并打印以下内容:

0                # n0
['sheet1']       # x
1                # n1
['sheet2']       # x
2                # n2
['sheet3']       # x
3                # n3
['sheet4']       # x

There are two flavors to get your sheetname and sheetnumber correct for input in the ws = ..code : 有两种方法可以使ws = ..code中的输入正确获取工作表名称和工作表ws = ..code

sheet_name = ['sheet1', 'sheet2', 'sheet3', 'sheet4']

for sheet in sheet_name:

    # option 1 to get sheetname and sheet number

    sheetname = ''
    sheetnumber = ''
    for x in sheet:
        print (x)                  # opt.1 print 1
        if x is not int:
            sheetname +=x
        else:
            sheetnumber + x

    print (sheetname, sheetnumber) # opt.1 print 2


    # option 2 to get sheetname and sheet number

    name, value = sheet[0:5], sheet[5:]
    print (name, value)

    # ... your code ...continues here with linked option 1 or 2.

#    ws = writer.sheets[sheet_name[n]]
#    ws.conditional_format('H2:H100', {'type': 'data_bar',
                                       #'bar_solid': True})

#writer.save()   # <-- check if...

...user Carsten is correct about indentation for this!! ...用户Carsten对此缩进是正确的!

The printed result from both options: 这两个选项的打印结果:

s         # opt.1 ..print 1
h
e
e
t
1
sheet1    # opt.1 ..print 2 
sheet 1   # opt.2
s         # opt.1 ..print 1
h
e
e
t
2
sheet2    # opt.1 ..print 2 
sheet 2   # opt.2

...snippet ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 循环通过 excel 工作表并根据条件将每个工作表保存到 csv - Loop through excel sheets and save each sheet into a csv based on a condition 如何遍历 excel 工作表并使用 python 在每张工作表上执行相同的任务 - How to loop through excel sheets and perform same task on each sheet using python 如何遍历 selenium python 表中 excel 表中的所有条目 - how to loop through all the entries in excel sheet in selenium python 如何使用 openpyxl 遍历 Excel 表的行? - How to loop through rows of the Excel sheet using openpyxl? 在Excel工作表中循环的Python for循环 - Python for loop that cycles through a excel sheet 在 Python 中,遍历工作表并仅计算第一列中的非 NaN 单元格。 每张纸的输出名称和找到的总数 - In Python, loop through sheet and only count Non-NaN cells in the first column. Output name of each sheet and total found Python Pandas - 循环浏览 Excel 文件的文件夹,将每个 ZC1D81AF583580.x 文件的数据导出到它们自己的文件“DED8FDCZ”中 - Python Pandas - loop through folder of Excel files, export data from each Excel file's sheet into their own .xlsx file 如何使用openpyxl遍历excel电子表格中的每一行 - how to loop through each row in excel spreadsheet using openpyxl 如何将 for 循环生成的每个数据帧保存到同一个 Excel 工作表,但不同的工作表? - How to save each dataframe produced by a for loop to the same excel sheet, but different sheets? 如何将每个表保存在单独的 excel 表中 - How to save each table in a separate excel sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM