将图片批量导入excel的B列,A列为图片名,请问为什么给A列单元格赋值,会报错:[WinError 32] 另一个程序正在使用此文件,进程无法访问

代码已被折叠,点此展开

报错为

如果我把(excel_data [‘A’ + str (x)] = str (pic_name))这行删掉或者故意把单元格的位置写错,让程序运行 except 的代码,程序都可以正常运行,有大佬知道是为什么吗,感谢 ^_^

Jason990420
最佳答案
File "E:\Python\lib\site-packages\openpyxl\worksheet\_writer.py", line 177, in rows
    return sorted(rows.items())
TypeError: '<' not supported between instance of 'str' and 'int'

Check the values of rows in _writer.py, something like this, or add statements to _writer.py to check.

from collections import defaultdict
import openpyxl

wb = openpyxl.Workbook()
ws = wb.create_sheet('Sheet 1')

for i in range(5):
    ws[f'A{i+1}'] = i+1
    ws.row_dimensions[str(i+1)].height = 20

# code from method 'rows' in '_writer.py'
rows = defaultdict(list)
for (row, col), cell in sorted(ws._cells.items()):
    rows[row].append(cell)

for row in ws.row_dimensions.keys() - rows.keys():
    rows[row] = []
# Check ths value of 'rows'
for key, value in rows.items():
    print(f'{repr(key)} : {value}')
1 : [<Cell 'Sheet 1'.A1>]
2 : [<Cell 'Sheet 1'.A2>]
3 : [<Cell 'Sheet 1'.A3>]
4 : [<Cell 'Sheet 1'.A4>]
5 : [<Cell 'Sheet 1'.A5>]
'2' : []
'3' : []
'4' : []
'1' : []
'5' : []

Found two kinds of keys used, str and int

That’s why you got exception which happened when return sorted(rows.items())

So, I got that you used wrong row number in some statements, then it is excel_data.row_dimensions[str(x)].

Revise this statement from

excel_data.row_dimensions[str(x)].height = 1280 / 10 * 0.8

to

excel_data.row_dimensions[x].height = 1280 / 10 * 0.8
2年前 评论
Jason990420 (作者) 2年前
hg946902208 (楼主) 2年前
hg946902208 (楼主) 2年前
讨论数量: 4
Jason990420
File "E:\Python\lib\site-packages\openpyxl\worksheet\_writer.py", line 177, in rows
    return sorted(rows.items())
TypeError: '<' not supported between instance of 'str' and 'int'

Check the values of rows in _writer.py, something like this, or add statements to _writer.py to check.

from collections import defaultdict
import openpyxl

wb = openpyxl.Workbook()
ws = wb.create_sheet('Sheet 1')

for i in range(5):
    ws[f'A{i+1}'] = i+1
    ws.row_dimensions[str(i+1)].height = 20

# code from method 'rows' in '_writer.py'
rows = defaultdict(list)
for (row, col), cell in sorted(ws._cells.items()):
    rows[row].append(cell)

for row in ws.row_dimensions.keys() - rows.keys():
    rows[row] = []
# Check ths value of 'rows'
for key, value in rows.items():
    print(f'{repr(key)} : {value}')
1 : [<Cell 'Sheet 1'.A1>]
2 : [<Cell 'Sheet 1'.A2>]
3 : [<Cell 'Sheet 1'.A3>]
4 : [<Cell 'Sheet 1'.A4>]
5 : [<Cell 'Sheet 1'.A5>]
'2' : []
'3' : []
'4' : []
'1' : []
'5' : []

Found two kinds of keys used, str and int

That’s why you got exception which happened when return sorted(rows.items())

So, I got that you used wrong row number in some statements, then it is excel_data.row_dimensions[str(x)].

Revise this statement from

excel_data.row_dimensions[str(x)].height = 1280 / 10 * 0.8

to

excel_data.row_dimensions[x].height = 1280 / 10 * 0.8
2年前 评论
Jason990420 (作者) 2年前
hg946902208 (楼主) 2年前
hg946902208 (楼主) 2年前