我在使用load_workbook()读取excel获取内容后,再将内容保存为txt文件时遇到下面这栏报错
File "D:/python/excel_py.py", line 58, in sdx
yr = open('C:\iyb\ty.txt', 'w',
TypeError: load_workbook() got an unexpected keyword argument 'encoding'
代码如下
from openpyxl import *
from openpyxl.utils import get_column_letter, column_index_from_string
def excel_cellname():
wb = load_workbook('C:/Users/USER/Desktop/20210709.xlsx')
ws = wb.active
maxRow = ws.max_row # 获取最大行数
maxColumn = ws.max_column # 获取最大列数
# print(ws['B3'].value)
# print(maxRow, range(maxColumn))
data_hang = []
char = []
charrow = []
if maxRow > 2 and maxColumn > 2:
for i in range(maxRow):
for j in range(maxColumn):
try:
if ws.cell(i, j).value != None:
char.append(j)
# print(ws.cell(i,j).value)
if ws.cell(i, j).value == "jk" and ws.cell(i, j + 1).value != None:
# print(i, j)
# print(ws.cell(i, j))
charrow.append(i)
data_hang.append(ws.cell(i, j + 2).value.replace("\n", ""))
except:
pass
# continue
# print(data_hang)
print(charrow)
print(get_column_letter(max(char)))
print(get_column_letter(min(char) + 1))
Cellmin = get_column_letter(min(char) + 1)
Cellmax = get_column_letter(max(char))
# print(ws["A1:D10"])
charname = []
for cr in charrow:
# print(ws['B' + str(cr) + ':' + 'G' + str(cr)])
for sw in ws[Cellmin + str(cr) + ':' + Cellmax + str(cr)]:
for wf in sw:
if wf.value != None:
# print(wf.value.replace("\n", ""))
charname.append(wf.value.replace("\n", ""))
sdd = "\n".join(charname)
er = sdd.replace("\n", "")
qe = er.replace(" ", "")
return qe
def sdx():
xc = excel_cellname()
print(xc)
yr = open('C:\iyb\ty.txt', 'w',
encoding='UTF-8')
yr.write(xc)
yr.flush()
sdx()
报错信息里面说的好像是load_workbook得到了一个不应该得到的参数encoding,检查一下你的代码把
wb = load_workbook(r'simple_data.xlsx')
ws = wb.active
with open('1.txt', mode='w') as f:
for row in ws:
for cell in row:
f.write(f'{cell.value},')
f.write('\n')