使用Python操做excel文件

依賴環境

讀取excel表裏的數據,須要依賴的包是xlrd,首先須要安裝xlrd包python

pip3 install xlrd數據庫

簡單的經常使用命令

複製代碼
#coding=utf-8 import xlrd
'''
文件路徑比較重要,要以這種方式去寫文件路徑不用
''' file_path = r'd:/功率因數.xlsx' #讀取的文件路徑 file_path = file_path.decode('utf-8') #文件中的中文轉碼 data = xlrd.open_workbook(file_path) #獲取數據 table = data.sheet_by_name('歷史數據') #獲取sheet nrows = table.nrows #獲取總行數 ncols = table.ncols #獲取總列數 table.row_values(i) #獲取一行的數值 table.col_values(i) #獲取一列的數值 #獲取一個單元格的數值 cell_value = table.cell(a,b).value
複製代碼

 本地excel文件測試版本

#-*- coding=utf-8 -*- import xlrd def open_excel(file= 'file.xls'): try: data = xlrd.open_workbook(file) return data except Exception as e: print(str(e)) def excel_table_byname(file= u'D:\\test\\學信網zh.xlsx',colnameindex=0,by_name=u'Sheet1'):#修改本身路徑 data = open_excel(file) table = data.sheet_by_name(by_name) #得到表格 nrows = table.nrows # 拿到總共行數 colnames = table.row_values(colnameindex) # 某一行數據 ['姓名', '用戶名', '聯繫方式', '密碼'] list = [] for rownum in range(1, nrows): #也就是從Excel第二行開始,第一行表頭不算 row = table.row_values(rownum) if row: app = {} for i in range(len(colnames)): app[colnames[i]] = row[i] #表頭與數據對應 list.append(app) return list def main(): tables = excel_table_byname() for row in tables: print(row) if __name__ =="__main__": main()

讀取excel文件錄入數據庫

代碼示例app

複製代碼
# coding: utf-8 from __future__ import unicode_literals from questionnaire.models import Paper, QuestionnaireTag, QuestionnaireAnswer, ChoiceQuestion, QuestionnaireQuota, \ SubjectQuestion import xlrd import os from education import settings def choicequestion_init(): # 遍歷文件夾 file_package = os.listdir(os.path.join(os.path.join(settings.BASE_DIR, 'education', 'init', 'choicequestion'))) for file in file_package: if not os.path.isdir(file): # 判斷是不是文件夾,不是文件夾纔打開 workbook = xlrd.open_workbook(os.path.join( os.path.join(settings.BASE_DIR, 'education', 'init', 'choicequestion') + "/" + file)) # 打開文件 sheet_names = workbook.sheet_names() # 循環Excel文件的全部行 for sheet in sheet_names: sheet = workbook.sheet_by_name(sheet) if sheet.name: tag_obj = QuestionnaireTag.objects.create(name=sheet.name) # 建立試卷 paper_obj = Paper() paper_obj.name = tag_obj.name paper_obj.questionnairetag = tag_obj paper_obj.save() last_obj = None for index, row in enumerate(sheet.get_rows()): if index == 0: continue if row[0].value: if not QuestionnaireQuota.objects.filter(name=row[0].value): parent_obj = QuestionnaireQuota.objects.create(name=row[0].value) if not QuestionnaireQuota.objects.filter(name=row[1].value): children_obj = QuestionnaireQuota.objects.create(name=row[1].value, parent=parent_obj) last_obj = parent_obj else: if not QuestionnaireQuota.objects.filter(name=row[0].value): children_obj = QuestionnaireQuota.objects.create(name=row[1].value, parent=last_obj) type_name = "" if row[3].value == "單選": type_name = "single" elif row[3].value == "多選": type_name = "multiple" is_textfield = False if row[4].value == "": is_textfield = False elif row[4].value == "": is_textfield = True ans = {} num = 65 for i in range(5, len(row)): ans["option_%s" % (chr(num))] = row[i].value num += 1 question_obj = ChoiceQuestion.objects.create(stem=row[2].value, type=type_name, is_textfield=is_textfield, **ans) question_obj.questionnairequotas.add(children_obj) question_obj.questionnairetags.add(tag_obj) question_obj.save() paper_obj.choicequestions.add(question_obj) paper_obj.save()
複製代碼
相關文章
相關標籤/搜索