如何用openpyxl读取表格内容,再将获取的内容保存为.txt文件

毕设客栈 其他问答 1

我在使用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'

代码如下

- coding: utf-8 --
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()

回复

共1条回复 我来回复
  • 源码驿站
    这个人很懒,什么都没有留下~
    评论

    报错信息里面说的好像是load_workbook得到了一个不应该得到的参数encoding,检查一下你的代码

    0条评论

发表回复

登录后才能评论