Python操作Excel實例(封裝從json讀文件函數,將其存入excel函數)

目錄

實例一 把列表內容寫入Excel

實例二 將excel中的內容讀到實例一的lst中

 封裝——從json讀文件函數Rjson,將其存入excel函數W_inExcel


實例一 把列表內容寫入Excel

#!/usr/bin/python
#-*-coding:utf-8-*-
import xlrd
import xlwt

wbook = xlwt.Workbook( )
wsheet = wbook.add_sheet("列表存入表")
""" :type :xlwt.Worksheet"""
lst = [{"id": 1, "name": "虞姬", "age": 25},
	   {"id": 2, "name": "王昭君", "age": 21,"住址":"南京"},
	   {"id": 3, "name": "成吉", "age": 265,"住址":"鎮江"},
	   {"id": 4, "name": "魯班", "age": 43}]
#選取最多的key作爲表頭
len_key = len(lst[0].keys())
for i in range(0,len(lst)):
	if len(lst[i].keys())>len_key:
		len_key=len(lst[i].keys())
		num=i
#表中寫入表頭
lst_key=list(lst[num].keys())
for i in range(len(lst_key)):
	wsheet.write(0,i,lst_key[i])
#循環寫入表
for i in range(len(lst)):
	lst_value=list(lst[i].values())
	for j in range(len(lst_value)):
		wsheet.write(i+1,j,lst_value[j])
wbook.save("E:\\工作學習\\工作前自學\\Python\\excel_study\\excel_python.xls")

最後得到結果

實例二 將excel中的內容讀到實例一的lst中

#!/usr/bin/python
#-*-coding:utf-8-*-
import xlrd
import xlwt
"""將表中的內容寫入字典list"""
wb=xlrd.open_workbook("E:\\工作學習\\工作前自學\\Python\\excel_study\\excel_python.xls")
wsheet=wb.sheet_by_index(0)
rowNum=wsheet.nrows
lst=[]
dic=dict.fromkeys(wsheet.row_values(0))
for i in range(rowNum):
	for j in range(wsheet.row_len(i)):
		dic[wsheet.cell_value(0,j)]=wsheet.cell_value(i,j)
	lst.append(dic)
print(lst)

最後結果

 封裝——從json讀文件函數Rjson,將其存入excel函數W_inExcel

#!/usr/bin/python
#-*-coding:utf-8-*-
import xlwt
import json

"""從本地文件讀json文件,針對格式爲[{},{}],且每個{}中的key相同,讀到表格中key爲第一行表頭。
如果格式只有一個字典則變爲[{}]"""
def Rjson(adress_file):
	try:
		with open(adress_file) as file_json:
			print("成功打開文件%s" % adress_file)
			json_load=json.load(file_json)
			lst_dict=json_load
	except IOError:
		print("文件打開失敗,%s 文件不存在" % adress_file)
	else:
		if type(json_load) is dict:
			lst=[]
			lst.append(json_load)
			lst_dict =lst
		return lst_dict



"""	將上述讀出的json_load放入excel,由於每個字典的keys都相同,故放第一行做表頭"""
def W_inExcel(json_load,sheet_name,save_path):
	wbook = xlwt.Workbook()
	wsheet = wbook.add_sheet("sheet_name")
	""" :type :xlwt.Worksheet"""
	# 選取最多的key作爲表頭
	len_key = len(json_load[0].keys())
	num=0   ###記錄keys最多的一行
	for i in range(len(json_load)):
		if len(json_load[i].keys()) > len_key:
			len_key = len(json_load[i].keys())
			num = i
	# 表中寫入表頭
	lst_key = list(json_load[num].keys())
	print(lst_key)
	for i in range(len(lst_key)):
		wsheet.write(0, i, lst_key[i])
	# 循環寫入表
	for i in range(len(json_load)):
		lst_value = list(json_load[i].values())
		for j in range(len(lst_value)):
			if lst_value[j] is  dict or list:   ##放入Excel中的元素類型有限,轉換爲string
				lst_value[j]=str(lst_value[j])
			wsheet.write(i + 1, j, lst_value[j])
	wbook.save(save_path)



if __name__ == '__main__':
	adress_file="E:\\工作學習\\工作前自學\\Python\\record.json"
	save_path="E:\\工作學習\\工作前自學\\Python\\excel_study\\excel_python1.xls"
	sheet_name="存入"
	lst=Rjson(adress_file)
	print(lst)
	W_inExcel(lst, sheet_name, save_path)

輸入爲 

輸出爲 

和 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章