python 读取 xls文件

编程入门 行业动态 更新时间:2024-10-08 14:49:53

python 读取 xls<a href=https://www.elefans.com/category/jswz/34/1771438.html style=文件"/>

python 读取 xls文件

import sys
import xlrd
#from xlrd import open_workbook
#import pandas as pd
import xlwt
file_path = 'D:\data\\gmy\\文学\\傲慢与偏见.xls'
file = pd.read_html(file_path)#获取.xls工作簿对象
'''
sheet_name = file.sheet_names()#获取所有工作表名称
print(sheet_name)sheet = file.sheet_by_index(1)#根据索引获取工作表对象
print(sheet)
sheet = file.sheet_by_name('Sheet1')#根据名称获取工作表对象
print(sheet)
print('------------')
rows = sheet.nrows#获取工作表行数
cols = sheet.ncols#获取工作表列数
print(rows)
print(cols)row = sheet.row_values(1)#获取工作表某一行的内容
print(row)cell = sheet.cell_value(0,1)#获取工作表某一单元格的内容
print(cell)
print('\n')
'''
def xls_pro(file, pro_file_com):file = xlrd.open_workbook(file)sheet_names = file.sheet_names()with open(pro_file_com, 'w', encoding = 'utf-8') as f1:for sheet_name in sheet_names:sheet = file.sheet_by_name(sheet_name)rows = sheet.nrowsprint(rows)#cols = sheet.ncolsif rows:for i in range(rows):cell0 = str(sheet.cell_value(i, 0))#f1.write(cell0 + '\n')cell1 = str(sheet.cell_value(i, 1))f1.write(cell0 + ' ||| ' + cell1 + '\n')file = 'D:\\data\\gmy\\文学\\傲慢与偏见.xls'
pro_file_com = "D:\\data\\xls_com.txt"
#pro_file_chn = "D:\\data\\xls_chn.txt"pro = xls_pro(file, pro_file_com)

更多推荐

python 读取 xls文件

本文发布于:2024-03-23 15:10:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1739617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   python   xls

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!