本文目錄一覽:
- 1、如何用python讀取excel文件?
- 2、Python的excel讀取和寫入
- 3、如何用python讀取excel
- 4、python怎麼讀寫當前的excel
- 5、怎樣用python,讀取excel中的一列數據
如何用python讀取excel文件?
1.首先說明我是使用的python3.5,我的office版本是2010,首先打開dos命令窗,安裝必須的兩個庫,命令是:
pip3 install xlrd
Pip3 install xlwt
2.準備好excel,例如我的一個工作文件,我放在D盤/百度經驗/11.xlsx,只有一個頁簽A,內容是一些銷售數據
3.打開pycharm,新建一個excel.py的文件,首先導入支持庫
import xlrdimport xlwt
4.針對剛入門的新手,先介紹三個知識,第一個:獲取excel的sheet名稱,第二:獲取excel行數與列數,第三:獲取第幾行第幾列的具體值,這是最常用的三個知識點
5.貼出代碼,具體分析:
(1)要操作excel,首先得打開excel,使用open_workbook(‘路徑’)
(2)要獲取行與列,使用nrows(行),ncols(列)
(3)獲取具體的值,使用cell(row,col).value
workbook=xlrd.open_workbook(r’E:11.xlsx’)print (workbook.sheet_names()) sheet2=workbook.sheet_by_name(‘A’) nrows=sheet2.nrows ncols=sheet2.ncols print(nrows,ncols) cell_A=sheet2.cell(1,1).value print(cell_A)
6.要在excel里寫入值,就要使用write屬性,重點說明寫入是用到xlwt這個支援庫,思路是先新建excel,然後新建頁簽B,然後將一組數據寫入到B,最後保存為excel.xls,這裡建議保存為2003的格式,大部分電腦都能打開,特別注意保存的excel的路徑是在python工作文件的目錄下面,貼出代碼:
stus = [[‘年’, ‘月’], [‘2018′, ’10’], [‘2017’, ‘9’], [‘2016’, ‘8’]]Excel = xlwt.Workbook() # 新建excelsheet = Excel.add_sheet(‘B’) #新建頁簽Brow = 0for stu in stus: col = 0 for s in stu: sheet.write(row, col, s) #開始寫入 col = col + 1 row = row + 1Excel.save(‘Excel.xls’) #保存
關於如何用python讀取excel文件,環球青藤小編就和大家分享到這裡了,學習是永無止境的,學習一項技能更是受益終身,所以,只要肯努力學,什麼時候開始都不晚。如果您還想繼續了解關於python編程的學習方法及素材等內容,可以點擊本站其他文章學習。
Python的excel讀取和寫入
現在常用的處理excel的方法大多是numpy,但是之前已經習慣了用xlrd的工具,所以也記錄一下祖傳的excel讀取/創建/寫入:
1.讀取excel:
2.創建一個excel:
3.寫入excel:
如何用python讀取excel
用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:普通字體”’
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;’)
python怎麼讀寫當前的excel
python有很強大的excel讀寫能力,只需要安裝xlrd,xlwt這兩個庫就可以了
pip install xlrd
Pip install xlwt
看教程,在右邊的鏈接:網頁鏈接
#ecoding=utf-8
import sys
reload(sys)
sys.setdefaultencoding(‘utf-8’)
from pyExcelerator import *
w = Workbook() #創建一個工作簿
ws = w.add_sheet(‘1’) #創建一個工作表
for j in range(0,5): #控制列
for i in range(0, 50000): #控制行
if(j == 0): #第一列
ws.write(i, j, ‘13001454722’)
if(j == 1):
ws.write(i,j,’6′)
if(j == 2):
ws.write(i, j, ‘KQ_201801_20WANONE’)
if(j == 3):
ws.write(i,j,’1′)
if(j == 4):
ws.write(i,j,u’否’)
w.save(‘xqtest.xls’)
怎樣用python,讀取excel中的一列數據
用python讀取excel中的一列數據步驟如下:
1、首先打開dos命令窗,安裝必須的兩個庫,命令是: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。
原創文章,作者:X5ONZ,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/128561.html