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 15:09:03 浏览: 19
根据您提供的代码和错误信息,可以看出是在以下代码行出错:
```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` 变量没有被正确赋值。这可能是因为在读取表格数据时,第一行没有包含所需的表头信息。因此,您需要检查一下数据表的第一行是否包含所需的表头信息,如果没有,请添加表头并重新运行代码。
相关问题
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 有表头“接收人工号”,和“推送内容”表头,不过“推送内容”为合并单元格,不知道会不会是读到空的表头造成的,代码怎么改进
根据错误提示可以看出是因为 `push_content_index` 变量为 `None` 导致的,而 `push_content_index` 是通过查找包含“推送内容”文本的表头来确定的。因此,可以检查一下表格中是否真正存在包含“推送内容”文本的表头,或者将查找表头的代码进行改进。
具体的代码改进方案如下:
```python
for row in sheet.rows:
values = [cell.value for cell in row]
if not any(values):
continue
if sheetname == "总行":
# 读取表头
if not headers:
headers = values
receiver_index = headers.index('接收人工号')
push_content_index = [i for i, header in enumerate(headers) if '推送内容' in str(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 = ''
```
在上述改进中,我们对查找表头的代码做了修改,使用了 `str()` 函数将表头数据转换为字符串类型,以避免在查找时出现类型不匹配的问题。
修改以下代码错误:from openpyxl import load_workbook import unicodedata # 指定表格文件名列表 filenames = ['file1.xlsx', 'file2.xlsx', 'file3.xlsx'] # 使用列表推导式读取多个表格 workbooks = [load_workbook(filename) for filename in filenames] # 遍历每个工作表 for sheetname in workbooks.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 文件 workbooks.save(filename='example1.xlsx')
以下是经过修正后的代码:
```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)
# 保存修改后的 Excel 文件
workbook.save(filename='example1.xlsx')
```
修正后,代码中的问题包括:
1. `from openpyxl import load_workbook import unicodedata` 这一行代码中应该将 `import unicodedata` 移到 `from openpyxl import load_workbook` 的下一行。
2. `workbooks.sheetnames` 应该修改为 `workbook.sheetnames`,因为在遍历时,变量名 `workbook` 代表的是一个具体的 Excel 表格对象。
3. 将第一行代码中的 `load_workbook` 移动到循环中,以便对每个 Excel 表格进行操作。