您好,登錄后才能下訂單哦!
python3.7 openpyxl 刪除指定一列或者一行
# encoding:utf-8 import pandas as pd import openpyxl xl = pd.read_excel(r"E:\55\CRM經營分析表-10001741-1570416265044.xls") xl.to_excel(r"E:\55\crms.xlsx") wk = openpyxl.load_workbook(r"E:\55\crms.xlsx") #加載已經存在的excel wk_name = wk.sheetnames wk_sheet = wk[wk_name[0]] wk_sheet.cell(2,2,value='大區') wk_sheet.cell(2,3,value='小區') wk_sheet.cell(2,4,value='店鋪編碼') wk_sheet.cell(2,5,value='店鋪名稱') """ 以上都是讀取的代碼,看不懂可以看我之前的博客文章 ,下面才是正文內容""" wk_sheet.delete_rows(3,2) #刪除從第一行開始算的2行內容 wk_sheet.delete_cols(1,2) #刪除從第一列開始算的2列內容 wk.save(r"E:\55\s.xlsx")
知識點擴展:
python3 openpyxl基本操作,具體代碼如下所示:
#coding:utf-8 import xlrd import xlwt # 讀寫2007 excel import openpyxl import sys #讀取設備sn # def readSN(path): # wb = openpyxl.load_workbook(path) # sheet = wb.active # dict = [] # for i in range(2, sheet.max_row +1): # c = sheet["C" + str(i)].value; # d = sheet["D" + str(i)].value; # # dict.append(d) # #dict.append(d) # #print(c,d) # return dict; # # pass; # print(readSN("./sim/1.xlsx")) def read07Excel(path,path2): wb = openpyxl.load_workbook(path) sheet = wb.active # print(sheet.max_column) # 獲取最大列數 # print(sheet.max_row) # 獲取最大行數 #print(sheet['B1'].value) wb1 = openpyxl.load_workbook(path2) sheet1 = wb1.active for i in range(2,sheet.max_row): iccid = sheet["B"+str(i)].value; len_iccid = len(iccid) if len_iccid == 20 : sub_iccid = iccid[16:-1] elif len_iccid == 21: sub_iccid = iccid[17:-1] for x in range(1,sheet1.max_row): #print(sheet1["D"+str(x)].value) if sub_iccid+"N" == sheet1["D"+str(x)].value: sheet["O"+str(i)].value = sheet1["C"+str(x)].value; wb.save(filename=path) print(str(sheet1["D"+str(x)].value) + " "+ str(sheet1["C"+str(x)].value) +" "+ str(iccid)) print() pass # 寫入數據 # s =sheet["P"+str(i)].value = "dsdaf"; # wb.save(filename=path) # p = sheet["P" + str(i)].value; #print(sub_iccid) # for row in sheet.rows: # for cell in row: # print(cell.value, "\t", end="") # print(cell.column, "\t", end="") # # # print() # sys.exit() # path = "./sim/2.xlsx" # wb = openpyxl.load_workbook(path) # #sheet = wb.sheetnames[0] #獲取名稱 # sheet = wb.active # 分別返回 #print(sheet['A1'].value) #獲取單元格A1值 read07Excel("./sim/2.xlsx","./sim/1.xlsx") # wb=openpyxl.load_workbook('./sim/1.xlsx') #打開excel文件 # print(wb.sheetnames) #獲取工作簿所有工作表名
總結
以上所述是小編給大家介紹的python3.7 openpyxl 刪除指定一列或者一行的代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。