帮我修改一下原来的代码吧
时间: 2024-04-12 16:32:18 浏览: 277
当然,我可以帮您修改代码。以下是修改后的代码:
```python
import os
import openpyxl
os.chdir('D:\\')
wb = openpyxl.load_workbook('sj2.xlsx')
shet1 = wb['Sheet1']
shet3 = wb['Sheet3']
A = 'x'
B = 'y'
C = 'z'
D = 'w'
Dicttbk = {2: A, 3: B, 4: C, 5: D}
def InGrades(col, strings):
for rownum3 in range(2, shet3.max_row + 1):
shet3Name = shet3.cell(row=rownum3, column=1).value
shet3val = shet3.cell(row=rownum3, column=col).value
if shet3val != '/':
for rownum1 in range(2, shet1.max_row + 1):
shet1Name = shet1.cell(row=rownum1, column=5).value
shet1val = shet1.cell(row=rownum1, column=4).value
if (shet1Name == shet3Name) and (shet1val == strings):
shet1.cell(row=rownum1, column=18).value = shet3val
if __name__ == '__main__':
for key, value in Dicttbk.items():
InGrades(key, value)
wb.save('D:\\new sj.xlsx')
```
主要的修改包括:
1. 调整了循环的范围,将`range(2, shet3.max_row + 2)`修改为`range(2, shet3.max_row + 1)`,避免超出范围。
2. 在主程序中,直接调用`InGrades(key, value)`,无需再次调用`InGrades(count, value)`。
请注意,在运行修改后的代码之前,确保已经安装了openpyxl库。另外,如果仍然遇到问题,请提供具体的错误信息,以便我能更进一步地帮助您。
阅读全文