for sheetname in wb.sheetnames: sheet = wb[sheetname] # 判断sheet表是否为“总行” if sheetname == "总行": # 读取表头为“接收人工号”和“推送内容”的数据 for row in sheet.rows: values = [cell.value for cell in row] if not any(values): continue # 读取表头 if not headers: headers = values receiver_index = headers.index('接收人工号') push_content_index = [i for i, header in enumerate(headers) if '推送内容' in header] continue # 读取数据 receiver = values[receiver_index] for index in push_content_index: push_content = values[index] if push_content: strtext += push_content # 将数据存储到字典中 data_dict[receiver] = strtext strtext = '' 报错argument of type "nonetype" is not iterable
时间: 2024-02-14 11:09:03 浏览: 117
根据您提供的代码和错误信息,可以看出是在以下代码行出错:
```python
if not headers:
headers = values
receiver_index = headers.index('接收人工号')
push_content_index = [i for i, header in enumerate(headers) if '推送内容' in header]
continue
```
错误信息指出 `headers` 是 `NoneType` 类型,即 `headers` 变量没有被正确赋值。这可能是因为在读取表格数据时,第一行没有包含所需的表头信息。因此,您需要检查一下数据表的第一行是否包含所需的表头信息,如果没有,请添加表头并重新运行代码。
相关问题
from openpyxl import load_workbook import unicodedata # 指定表格文件名列表 filenames = ['file1.xlsx', 'file2.xlsx', 'file3.xlsx'] # 使用列表推导式读取多个表格 workbooks = [load_workbook(filename) for filename in filenames] # 遍历每个工作表 for workbook in workbooks: for sheetname in workbook.sheetnames: sheet = workbooks[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, wb in enumerate([wb1, wb2, wb3]): wb.save(filenames[i])
以下是经过修正后的代码:
```python
import unicodedata
from openpyxl import load_workbook
# 指定表格文件名列表
filenames = ['file1.xlsx', 'file2.xlsx', 'file3.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)
# 使用循环存储多个表格
for i, workbook in enumerate(workbooks):
workbook.save(filenames[i])
```
修正后,代码中的问题包括:
1. `from openpyxl import load_workbook import unicodedata` 这一行代码中应该将 `import unicodedata` 移到 `from openpyxl import load_workbook` 的下一行。
2. `workbooks.sheetnames` 应该修改为 `workbook.sheetnames`,因为在遍历时,变量名 `workbook` 代表的是一个具体的 Excel 表格对象。
3. 将保存 Excel 文件的代码移动到循环之外,以便遍历每个 Excel 表格之后再进行保存。
检查代码错误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)
你的代码有一个语法错误。在 `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)
```
这样修改后应该就可以正常运行了。
阅读全文