python?對excel交互工具的使用詳情
python 對excel的 讀入 與 改寫
(對比xlwt、openpyxl、xlrd)

- xlwt不支持寫xlsx文件。
- openpyxl不支持讀xls文件。
- 計劃任務xlrd支持讀xls,xlsx文件。
- 計劃任務推薦讀文件用xlrd,寫文件用openpyxl。
#一、xlrd 讀
# 1.引入庫& 下載庫 xlrd
pip install xlrd # 下載
pip show xlrd # 顯示版本
pip install xlrd==1.2.0 # 下載指定版本
import xlrd # 導入
workBook = xlrd.open_workbook('D:\project\info.xls', 'rb') # 打開文件
workBook = xlrd.open_workbook(r'D:\project\info.xls')
allSheetNames = workBook.sheet_names() # 獲取所有sheet的名字(list類型)
SheetName1= workBook.sheet_names()[0] # 按索引號
print(allSheetNames, SheetName1)
#輸出:
['Sheet1', 'Sheet2', 'Sheet3'] Sheet1
# 獲取sheet內容
sheet1_content1 = workBook.sheet_by_index(0) # sheet索引從0開始
sheet1_content2 = workBook.sheet_by_name('sheet1') # 按sheet名字獲取
# 獲取整行和整列的值(數(shù)組)
print(sheet1_content1.name,sheet1_content1.nrows,sheet1_content1.ncols)
# 獲取整行和整列的值(數(shù)組)
rows = sheet1_content1.row_values(3) # 獲取第四行內容
cols = sheet1_content1.col_values(2) # 獲取第三列內容
print(rows)
print(cols )
# 獲取單元格內容(三種方式)
print(sheet1_content1.cell(1, 0).value)
print(sheet1_content1.cell_value(2, 2))
print(sheet1_content1.row(2)[2].value)
二、python 寫入數(shù)據(jù)
1 、 xlwt包寫入Excel文件
xlwt 寫庫的局限性: 只能寫入新建的 excel。
(寫入打開文檔 可用xlutils.copy的 copy 復制一份)
xlwt中生成的xls文件最多能支持65536行數(shù)據(jù)
創(chuàng)建表寫入數(shù)據(jù)
# 向execl中 批量寫入虛假數(shù)據(jù)
import xlwt,faker,random
wb=xlwt.Workbook()
sheet002=wb.add_sheet("002")
head=["姓名","年齡","性別"]
for h in head:
sheet002.write(0,head.index(h),h)
#利用for 循環(huán) 挨個寫入 數(shù)據(jù) 行,列,數(shù)據(jù)值 這里列使用下標即可
fake=faker.Faker()
for i in range(1,101):
sheet002.write(i, 0, fake.name())
sheet002.write(i, 1, random.randint(10,60))
sheet002.write(i, 2, random.choice(['男','女']))
wb.save("002.xls")
#2 復制表寫入數(shù)據(jù)
import xlwt
import xlrd
import xlutils.copy
rd = xlrd.open_workbook("Hello.xls", formatting_info = True) # 打開文件
wt = xlutils.copy.copy(rd) # 復制
sheets = wt.get_sheet(0) # 讀取第一個工作表
sheets.write(m, n, "I love you!") # 向 m-1 行 n-1 列的單元格寫入內容
wt.save("Hi.xls") # 保存2、openpyx 只可以讀xlsx 不可讀xls文檔
xl = openpyxl.load_workbook('D:\project\infoexcel.xlsx', data_only=True)
# 設置工作表
sheet1 = xl.worksheets[0]
for i in range(1, 24):
sheet1.cell(i, 3).value = cvalue
# 保存表格
xl.save('D:\project\infoexcel.xlsx')三、小結
python 提供excel交互的工具包很多,
- 一、 找到適合自己
- 二、 區(qū)分不同包的權限與功能
如:(xlrd 新版本不支持xlsx ,需要回滾老版本才可使用)
- 三、出現(xiàn)問題debug運行尋找問題點,合理盡快解決問題!
到此這篇關于python 對excel交互工具的使用詳情的文章就介紹到這了,更多相關python excel交互工具 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python Selenium Cookie 繞過驗證碼實現(xiàn)登錄示例代碼
這篇文章主要介紹了Python Selenium Cookie 繞過驗證碼實現(xiàn)登錄示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
django項目中使用云片網(wǎng)發(fā)送短信驗證碼的實現(xiàn)
這篇文章主要介紹了django項目中使用云片網(wǎng)發(fā)送短信驗證碼的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
Python過濾函數(shù)filter()使用自定義函數(shù)過濾序列實例
這篇文章主要介紹了Python過濾函數(shù)filter()使用自定義函數(shù)過濾序列實例,配合自定義函數(shù)可以實現(xiàn)許多強大的功能,需要的朋友可以參考下2014-08-08
Python開發(fā)游戲之井字游戲的實戰(zhàn)步驟
最近正在學習Python,所以最近做了一個關于Python的實例,下面這篇文章主要給大家介紹了關于Python開發(fā)游戲之井字游戲的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-02-02

