旅游網(wǎng)站建設(shè)的目的及功能定位優(yōu)幫云首頁推薦
·
作者簡介:一名在校計(jì)算機(jī)學(xué)生、每天分享Python的學(xué)習(xí)經(jīng)驗(yàn)、和學(xué)習(xí)筆記。?
?座右銘:低頭趕路,敬事如儀
個(gè)人主頁:網(wǎng)絡(luò)豆的主頁??????
目錄
前言
一.前幾章代碼?
1.獲取到第一題的選項(xiàng)單元格
2.實(shí)現(xiàn)批量獲取文件
二.?批量的讀取文件夾下所有Excel的數(shù)據(jù)
三.批量讀取某文件下的文件數(shù)據(jù)全部代碼
前言
本章將會(huì)繼續(xù)講解Python自動(dòng)化辦公案例前期回顧:?Python編程自動(dòng)化辦公案例(1)
Python編程自動(dòng)化辦公案例(2)
一.前幾章代碼?
1.獲取到第一題的選項(xiàng)單元格
import xlrd#1.打開張三xlsx excel工作簿
path = r"C:\Users\86134\PycharmProjects\zdh\zs.xlsx"
data = xlrd.open_workbook(path)#獲取第一個(gè)工作表對象
# table=data.sheets()#獲取到第一題的選項(xiàng)單元格
#注意: list index out of range 下標(biāo)超出索引范圍
#注意:里面的下標(biāo)也是從0開始
# table.cell_value(rowx=3,colx=4)#行,列下標(biāo)
2.實(shí)現(xiàn)批量獲取文件
#批量的讀取某文件夾下的每個(gè)Excel文件數(shù)據(jù)
#獲取某個(gè)文件下所有Excel文件的路徑
#標(biāo)準(zhǔn)庫 osimport ospath = r"C:\Users\86134\Desktop\zdh"
all_filename = os.listdir(path)all_filepath = []for filepath in all_filename:#鏈路拼接:filepath=os.path.join(path,filepath)print(filepath)#鏈路拼接:filepath=os.path.join(path,filepath)all_filepath.append(filepath)
print(all_filepath)
二.?批量的讀取文件夾下所有Excel的數(shù)據(jù)
前期我們進(jìn)行了所有文件的讀取,那么我們接下來將要開始讀取文件下的excel的數(shù)據(jù)。
for file in all_filepath:data =xlrd.open_workbook(file)table = data.sheets()[0]#獲取 用戶名 回答1 回答2#1.用戶名:從路徑中 提取 用戶名 劉六username = print(file.split("\\")[-1].split("."))#2.獲取回答1 模板固定的獲取單元格值auswerl1=table.cell_value(rowx=4,colx=4)#3.獲取回答2auswerl2=table.cell_value(rowx=10,colx=4)# print(username,auswerl1,auswerl2)
三.批量讀取某文件下的文件數(shù)據(jù)全部代碼
# 批量的讀取某文件夾下的每個(gè)Excel文件數(shù)據(jù)
# 獲取某個(gè)文件下所有Excel文件的路徑
# 標(biāo)準(zhǔn)庫 osimport os
import xlrdpath = r"C:\Users\Administrator\Desktop\自動(dòng)化"
all_filename = os.listdir(path)all_filepath = []for filepath in all_filename:# 鏈路拼接:filepath = os.path.join(path, filepath)print(filepath)# 鏈路拼接:filepath = os.path.join(path, filepath)all_filepath.append(filepath)
print(all_filepath)#初始化列表
content = []
for file in all_filepath:data =xlrd.open_workbook(file)table = data.sheets()[0]#獲取 用戶名 回答1 回答2#1.用戶名:從路徑中 提取 用戶名 劉六username = print(file.split("\\")[-1].split("."))#2.獲取回答1 模板固定的獲取單元格值auswerl1=table.cell_value(rowx=4,colx=4)#3.獲取回答2auswerl2=table.cell_value(rowx=10,colx=4)# print(username,auswerl1,auswerl2)#將數(shù)據(jù)以行為單位:用戶名,回答1,回答2temp = [username,auswerl1,auswerl2]#print(temp)content.append(temp)print(content)
創(chuàng)作不易,求關(guān)注,點(diǎn)贊,收藏,謝謝~?