读取txt文件,并保存到excel文件中
import linecache
import openpyxl
filename = r"需要打开的.txt文件"
myfile = open(filename)
lines = len(myfile.readlines())
workbook = openpyxl.Workbook()
sheet = workbook.active
for i in range(lines):
text=linecache.getline(filename,i+1) # .txt文件第i+1行内容
print(i+1,text)
sheet.cell(i+1, 1).value = text
workbook.save(filename.split("/")[-1].replace(".txt",".xlsx")) # 保存excel文件,并且文件名不变,只改变文件名后缀
读取excel表的行和列
import xlrd
data = xlrd.open_workbook(r'获取行、列的excel文件') # 打开excel表
table = data.sheets()[0] # 打开第一张表
print("行:", table.nrows)
print("列:", table.ncols)
print("第0行第1列的内容", table.cell_value(0,1))