用python读取excel中的一列数据步" />
乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 怎样用<em>python</em>,读取excel中的一列数据

怎样用<em>python</em>,读取excel中的一列数据

作者:乔山办公网日期:

返回目录:excel表格制作


XlsxWriter-master
xlrd
用这两个模块,一个写,一个读,非常方便。

用python读取excel中的一列数据步骤如下:

1、首先打开zddos命令窗,安装必须的两个库,命令是:pip3 install xlrd;Pip3 install xlwt。

2、准备好excel。

3、打开pycharm,新建一个excel.py的文件,首先导入支持库import xlrdimport xlwt。

4、要操作excel,首先得打开excel,使用open_workbook(‘路径’),要获取行与列,使用nrows(行),ncols(列),获取具体的值,使用cell(row,col).value。

5、要在excel里写入值,就要使用write属性,重点说明写入是用到xlwt这个支援库,思路是先新建excel,然后新建页签B,然后将一组数据写入到B,最后保存为excel.xls。


用python对excel的读写操作,要用到两个库:xlrd和xlwt,首先下载安装这两个库。

1、#读取Excel
import xlrd
data = xlrd.open_workbook(excelFile)
table = data.sheets()[0]
nrows = table.nrows #行数
ncols = table.ncols #列数
for i in xrange(0,nrows):
rowValues= table.row_values(i) #某一行数据
for item in rowValues:
print item

2、写Excel文件
'''往EXCEl单元格写内容,每次写一行sheet:页签名称;row:行内容列表;rowIndex:行索引;
isBold:true:粗字段,false:普通字体7a686964616fe78988e69d83338'''
def WriteSheetRow(sheet,rowValueList,rowIndex,isBold):
i = 0
style = xlwt.easyxf('font: bold 1')
#style = xlwt.easyxf('font: bold 0, color red;')#红色字体
#style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;') # 设置Excel单元格的背景色为黄色,字体为粗体
for svalue in rowValueList:
strValue = unicode(str(svalue),'utf-8')
if isBold:
sheet.write(rowIndex,i,strValue,style)
else:
sheet.write(rowIndex,i,strValue)
i = i + 1

'''写excel文件'''
def save_Excel(strFile):
excelFile = unicode(strFile, "utf8")
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet1',cell_overwrite_ok=True)
headList = ['标题1','标题2','标题3','标题4','总计']
rowIndex = 0
WriteSheetRow(sheet,headList,rowIndex,True)
for i in xrange(1,11):
rowIndex = rowIndex + 1
valueList = []
for j in xrange(1,5):
valueList.append(j*i)
WriteSheetRow(sheet,valueList,rowIndex,False)
wbk.save(excelFile)

style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;')

可以通过pip包管理器来安装Python包,但是pygame包包含C语言代码,需要进行编译。

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网