码迷,mamicode.com
首页 > 编程语言 > 详细

python基础教程:python3.7 openpyxl 删除指定一列或者一行的代码

时间:2020-04-14 12:27:26      阅读:361      评论:0      收藏:0      [点我收藏+]

标签:file   教程   open   pass   dsd   获取   row   需要   sheet   

这篇文章主要介绍了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) #获取工作簿所有工作表名

python基础教程:python3.7 openpyxl 删除指定一列或者一行的代码

标签:file   教程   open   pass   dsd   获取   row   需要   sheet   

原文地址:https://www.cnblogs.com/benming/p/12696941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!