当前位置:网站首页>Basic operations of xlrd and xlsxwriter

Basic operations of xlrd and xlsxwriter

2022-08-10 00:12:00 Chandler_river

import xlrd as xldata = xl.open_workbook("hypothetical data 0.xls")#Number of sheets in the workbookwork_sheets = data.nsheets#Name string of all tables in list formwork_names = data.sheet_names()#Get specific worksheet three wayssheet_index = 1work_sheet = data.sheets()[sheet_index]work_sheet = data.sheet_by_index(sheet_index)sheet_name = "Sheet2"work_sheet = data.sheet_by_name(sheet_name)#Check if the worksheet is importedcheck = data.sheet_loaded(sheet_name)check = data.sheet_loaded(sheet_index)#Get the worksheet namesheet_name = work_sheet.name#Get the number of valid rows/columns in the worksheetall_rows = work_sheet.nrows#Get a list of all cell objects in the specified rowrow_object = work_sheet.row(5)row_object2 = work_sheet.row_values(5)# cell data type#0:empty#1: text#2:number#3: date#4: boolean#5:errorrow_type = work_sheet.row_types(5)#Get the length of the valid cells in the specified rowrow_length = work_sheet.row_len(5)#get generator for all rows of worksheetrows_generator = work_sheet.get_rows()#Get the contents of the specified cellrowx = 1colx = 1cell_object = work_sheet.cell(rowx,colx)cell_value = work_sheet.cell_value(rowx,colx)cell_value2 = cell_object.value#read date#The date in excel is the number of days relative to the base time 1900-01-01 as the starting point (1)# Dates in Python are generally based on itcol_content = work_sheet.col_values(4)col_type = work_sheet.col_types(4)#0 means 1900-01-01 1 means 1904-01-01date_cell = xl.xldate_as_tuple(work_sheet.cell_value(4,4),0)date_cell = xl.xldate_as_datetime(work_sheet.cell_value(4,4),0)import xlsxwriter as xls#Create workbookworkbook = xls.Workbook("new_excel.xlsx")#Create worksheetworksheet = workbook.add_worksheet("sheet1")#data inputheadings = ["Number","testA","testB"]data = [[1,1,1],[2,2,2],[3,4,5],[5,6,7]]worksheet.write_row("A1",headings)worksheet.write_column("A2",data[0])worksheet.write_column("B2",data[1])worksheet.write_column("C2",data[2])#set the format of the cellworkfomat = workbook.add_format()workfomat.set_bold(1) #Set the border width to 1workfomat.set_num_format("0.00") #Format data to two decimal placesworkfomat.set_align("center") #alignmentworkfomat.set_fg_color("blue") #Set the background color#Merge Cellsworksheet.merge_range("D1:D7","What needs to be written in the cell")#change cell widthworksheet.set_column("D:E",30)#insert imageworksheet.insert_image("E1","absolute path")

原网站

版权声明
本文为[Chandler_river]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208092059008437.html