TypeError: cell() got an unexpected keyword argument 'value'
时间: 2023-07-31 12:04:44 浏览: 700
这个错误提示是在告诉你,`cell()` 方法不支持名为 `value` 的关键字参数。根据你的代码,可能是因为你想要使用的是 `openpyxl` 模块中的 `cell()` 方法,但是这个方法不支持 `value` 参数。
如果你想要设置某个单元格的值,可以使用工作表对象的 `cell()` 方法,但是它不支持关键字参数。你应该使用位置参数来指定单元格的行号和列号。例如,要将第一行第一列的单元格的值设为 `'Hello, world!'`,可以这样写:
```
dst_ws.cell(1, 1, 'Hello, world!')
```
如果你想要修改某个单元格的值,也应该使用这种方式来指定单元格的位置。例如,要将第一行第一列的单元格的值设为 `'Hello, world!'`,可以这样写:
```
dst_ws.cell(1, 1, 'Hello, world!')
```
因此,你的代码应该修改为下面这样:
```
for i in range(1, src_ws.nrows):
cell_value = dst_ws.cell(i, 0).value
dst_ws.cell(i + 1, 1, cell_value)
```
这样就可以避免使用不支持的关键字参数了。
相关问题
TypeError: cell() got an unexpected keyword argument 'row'
这个错误提示是在告诉你,`cell()` 方法不支持名为 `row` 的关键字参数。根据你的代码,可能是因为你想要使用的是 `openpyxl` 模块中的 `cell()` 方法,但是这个方法不支持 `row` 参数。
如果你想要获取某个单元格,可以使用工作表对象的 `cell()` 方法,但是它不支持关键字参数。你应该使用位置参数来指定单元格的行号和列号。例如,要获取第一行第一列的单元格,可以这样写:
```
cell_value = dst_ws.cell(1, 1).value
```
如果你想要修改某个单元格的值,也应该使用这种方式来指定单元格的位置。例如,要将第一行第一列的单元格的值设为 `'Hello, world!'`,可以这样写:
```
dst_ws.cell(1, 1, value='Hello, world!')
```
这样就可以避免使用不支持的关键字参数了。
TypeError: NDFrame.to_excel() got an unexpected keyword argument 'converters'
这个错误通常发生在尝试使用pandas DataFrame的`to_excel()`方法时,但是提供的参数不符合该方法的要求。`converters`参数是在较早版本的pandas中用于指定特定列数据转换的方式,但在新版本中(可能是0.24.0以上),这个参数已经被弃用,改用`categorical_dtype`或`options`来控制列的数据类型。
如果你仍然想要按字符类型写入数据,可以考虑以下替代方案:
1. **使用`astype`方法**:
在写入之前,对需要的列应用`astype(str)`或`astype('O')`将其转换为对象类型(Object)或字符串类型。
```python
df['Address'] = df['Address'].astype(str)
```
2. **使用`xlsxwriter`库**:
如果你需要更精细地控制格式,可以使用`xlsxwriter`库,它允许你在写入数据的同时设定单元格格式。
```python
import xlsxwriter
workbook = xlsxwriter.Workbook('output.xlsx')
worksheet = workbook.add_worksheet()
for i, row_data in df.iterrows():
for j, cell_value in enumerate(row_data):
worksheet.write(i, j, cell_value)
# 设置单元格为字符串格式
worksheet.set_column(j, j, None, {'num_format': '@'})
workbook.close()
```
在新版本的pandas中,`categorical_dtype`可以在`ExcelWriter`构造函数里设置列的类别类型:
```python
excel_writer = pd.ExcelWriter('output.xlsx', engine='openpyxl', options={'data_types': {'Address': object}})
```
阅读全文
相关推荐

















