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

Posted benming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础教程: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:55CRM经营分析表-10001741-1570416265044.xls")

xl.to_excel(r"E:55crms.xlsx")

wk = openpyxl.load_workbook(r"E:55crms.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:55s.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, " ", end="")

# print(cell.column, " ", 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 删除指定一列或者一行的代码的主要内容,如果未能解决你的问题,请参考以下文章

Mac下安装Python3.x版本

python安装教程(mac版)-python安装步骤

windows10上安装python3.7教程以及环境变量配置

全网最简约的Anaconda+Python3.7安装教程Win10(百分百成功)

python3.7-3.9安装mayavi教程

python3.7-3.9安装mayavi教程