python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python3.7 openpyxl 删除指定一列或者一行

python3.7 openpyxl 删除指定一列或者一行的代码

作者:最渣的黑客

这篇文章主要介绍了python3.7 openpyxl 删除指定一列或者一行,文中通过代码给大家介绍了python3 openpyxl基本操作,代码简单易懂,需要的朋友可以参考下

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,path1):
  wb = openpyxl.load_workbook(path)
  sheet = wb.active
  # print(sheet.max_column) # 获取最大列数
  # print(sheet.max_row) # 获取最大行数
  #print(sheet['B1'].value)
  wb1 = openpyxl.load_workbook(path1)
  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 删除指定一列或者一行的代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

您可能感兴趣的文章:
阅读全文