检查代码错误import unicodedata from openpyxl import load_workbook # 指定表格文件名列表 filenames = ['统计.xlsx'] # 使用列表推导式读取多个表格 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 = unicodedata.normalize('NFKC', cell.value) # 保存修改后的 Excel 文件 for i, workbook in enumerate(workbooks): filename = filenames[i].replace(".xlsx", "-1.xlsx") workbook.save(filename)
时间: 2024-01-16 07:02:10 浏览: 66
你的代码有一个语法错误。在 `import` 语句中,你需要使用 `as` 关键字来指定 `unicodedata` 模块的别名,如下所示:
```
import unicodedata as ud
from openpyxl import load_workbook
# 指定表格文件名列表
filenames = ['统计.xlsx']
# 使用列表推导式读取多个表格
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)
```
这样修改后应该就可以正常运行了。
阅读全文