網站首頁 編程語言 正文
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名字獲取
# 獲取整行和整列的值(數組)
print(sheet1_content1.name,sheet1_content1.nrows,sheet1_content1.ncols)
# 獲取整行和整列的值(數組)
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 寫入數據
1 、 xlwt包寫入Excel文件
xlwt 寫庫的局限性: 只能寫入新建的 excel。
(寫入打開文檔 可用xlutils.copy的 copy 復制一份)
xlwt中生成的xls文件最多能支持65536行數據
創建表寫入數據
# 向execl中 批量寫入虛假數據
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 循環 挨個寫入 數據 行,列,數據值 這里列使用下標即可
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 復制表寫入數據
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交互的工具包很多,
- 一、 找到適合自己
- 二、 區分不同包的權限與功能
如:(xlrd 新版本不支持xlsx ,需要回滾老版本才可使用)
- 三、出現問題debug運行尋找問題點,合理盡快解決問題!
原文鏈接:https://blog.csdn.net/qq_40761920/article/details/125819263
相關推薦
- 2022-02-19 C語言計算Robots機器人行走路線_C 語言
- 2021-11-26 Linux下查看IP地址不顯示解決辦法_Linux
- 2022-08-14 Python基礎教程之pip的安裝和卸載_python
- 2022-07-07 redis遠程連接不上的解決辦法_Redis
- 2022-07-12 Spring Boot項目下JPA自定義雪花算法ID生成器詳解
- 2022-11-04 LyScript實現Hook改寫MessageBox的方法詳解_python
- 2022-10-13 Python詳細介紹模型封裝部署流程_python
- 2023-09-17 adb shell cmd wifi(系統服務:wifi)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支