代码错误:import unicodedata as ud from openpyxl import load_workbook filenames = [f for f in os.listdir('.') if f.endswith('.xlsx')] # 指定表格文件名列表 dfs = [] for file in files: sheets = pd.read_excel(file, sheet_name=None) # 使用列表推导式读取多个表格 workbooks = [load_workbook(filename) for filename in filenames] # 遍历每个工作表 for workbook in workbooks: for sheetname in workbook.sheetnames: sheet = workbook[sheetname] # 遍历每个单元格 for row in sheet.iter_rows(): for cell in row: # 判断单元格是否为字符串类型 if isinstance(cell.value, str): # 将全角字符转换为半角字符 cell.value = ud.normalize('NFKC', cell.value) # 保存修改后的 Excel 文件 for i, workbook in enumerate(workbooks): filename = filenames[i].replace(".xlsx", "-1.xlsx") workbook.save(filename)
时间: 2024-01-28 22:05:45 浏览: 71
Leatrix_Latency_Fix_3.03_macroman.enc_FIX_Leatrix_Latency_Fix_la
5星 · 资源好评率100%
这段代码有一个比较明显的错误,第4行的 `files` 变量没有定义,应该改为 `filenames`,即:
```
filenames = [f for f in os.listdir('.') if f.endswith('.xlsx')] # 指定表格文件名列表
dfs = []
for file in filenames: # 修改此处的变量名
sheets = pd.read_excel(file, sheet_name=None) # 使用列表推导式读取多个表格
workbooks = [load_workbook(filename) for filename in filenames] # 遍历每个工作表
# ...后面的代码不变
```
阅读全文