Python自動化辦公之Excel數(shù)據(jù)的寫入
上一章節(jié)我們學(xué)習(xí)了 excel 的讀取模塊 - xlrd ,今天章節(jié)將學(xué)習(xí) excel 的寫入模塊 - xlsxwriter 。通過該章節(jié)的學(xué)習(xí),就可以自己主動生成 excel 文件了。
Excel 寫入 - xlsxwriter
xlsxwriter 的安裝
安裝方式:
pip install xlsxwriter
若安裝不上或者安裝速度過慢,可以換國內(nèi)的鏡像源地址:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xlsxwriter
導(dǎo)入:
import xlsxwriter
xlsxwriter 常用函數(shù)介紹
初始化 excel 對象
book = xlsxwriter.Workbook() # 生成 excel 對象 sheet = book.add.sheet(工作簿名稱) # 生成 excel 對象的工作簿名稱
獲取工作簿
| 函數(shù)名 | 說明 | 參數(shù) |
|---|---|---|
| xlsxwriter.Workbook() | 生成 excel 對象 | excel 文件名 |
| add_worksheet() | 添加工作簿 | 工作簿名稱 |
| sheet.write() | 書寫內(nèi)容 | 行索引、列索引、內(nèi)容 |
| book.close() | 關(guān)閉 excel 對象 | 無 |
代碼示例如下:
# coding:utf-8
import xlsxwriter # pip install xlsxwriter
excel = xlsxwriter.Workbook('write.xlsx') # 初始化 excel 對象
book = excel.add_worksheet('study') # 添加工作簿
title = ['姓名', '性別', '年齡', '成績', '等級'] # 定義 write.xlsx 的首行內(nèi)容
for index, data in enumerate(title): # for循環(huán) 利用枚舉函數(shù)將 title 的內(nèi)容寫入 "write.xlsx" 的首行
book.write(0, index, data)
excel.close()
運行結(jié)果如下:


小實戰(zhàn)
將項目中的 study.xlsx 文件的內(nèi)容寫入 write.xlsx
代碼示例如下:
# coding:utf-8
import xlsxwriter # pip install xlsxwriter
import xlrd
# excel = xlsxwriter.Workbook('write.xlsx') # 初始化 excel 對象
# book = excel.add_worksheet('study') # 添加工作簿
#
# title = ['姓名', '性別', '年齡', '成績', '等級'] # 定義 write.xlsx 的首行內(nèi)容
#
# for index, data in enumerate(title): # for循環(huán) 利用枚舉函數(shù)將 title 的內(nèi)容寫入 "write.xlsx" 的首行
# book.write(0, index, data)
# excel.close()
def read(): # 定義一個 read 函數(shù)讀取 "study.xlsx" 文件
result = []
excel = xlrd.open_workbook('study.xlsx')
book = excel.sheet_by_name('學(xué)生手冊')
for i in book.get_rows():
content = []
for j in i:
content.append(j.value)
result.append(content)
return result
def write(content): # 定義一個 write 函數(shù) 將讀取到 "study.xlsx" 的內(nèi)容寫入到 "write.xlsx" 文件
excel = xlsxwriter.Workbook('write.xlsx')
book = excel.add_worksheet('study')
for index, data in enumerate(content):
print(data) # 調(diào)試打印寫入的每一行內(nèi)容
for sub_index, sub_data in enumerate(data):
# print(sub_index, sub_data)
book.write(index, sub_index, sub_data)
excel.close()
if __name__ == '__main__':
result = read()
write(result)
運行結(jié)果如下:


到此這篇關(guān)于Python自動化辦公之Excel數(shù)據(jù)的寫入的文章就介紹到這了,更多相關(guān)Python Excel數(shù)據(jù)寫入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python數(shù)據(jù)結(jié)構(gòu)之棧、隊列及二叉樹定義與用法淺析
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之棧、隊列及二叉樹定義與用法,結(jié)合具體實例形式分析了Python數(shù)據(jù)結(jié)構(gòu)中棧、隊列及二叉樹的定義與使用相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
Python實現(xiàn)棧的方法詳解【基于數(shù)組和單鏈表兩種方法】
這篇文章主要介紹了Python實現(xiàn)棧的方法,結(jié)合實例形式詳細(xì)分析了Python基于數(shù)組和單鏈表兩種方法定義棧的相關(guān)操作技巧,需要的朋友可以參考下2020-02-02
Python 利用scrapy爬蟲通過短短50行代碼下載整站短視頻
近日,有朋友向我求助一件小事兒,他在一個短視頻app上看到一個好玩兒的段子,想下載下來,可死活找不到下載的方法。經(jīng)過我的一番研究才找到解決方法,下面小編給大家分享Python 利用scrapy爬蟲通過短短50行代碼下載整站短視頻的方法,感興趣的朋友一起看看吧2018-10-10
python爬蟲請求庫httpx和parsel解析庫的使用測評
這篇文章主要介紹了python爬蟲請求庫httpx和parsel解析庫的使用測評,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-05-05
pytorch中關(guān)于distributedsampler函數(shù)的使用
這篇文章主要介紹了pytorch中關(guān)于distributedsampler函數(shù)的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02

