语言:Python 3.7
学习时间:3天
适用工作:将excel中列剪切并复制
参考文档:https://openpyxl.readthedocs.io/en/stable/
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 9 14:23:00 2019
@author: zbliuA
"""
import openpyxl
import pandas as pd
filename = "2019年黄悦销售数据分析.xlsx"
wb = openpyxl.load_workbook(filename)
ws = wb['销售数据分析']
sheet = wb.active
a = sheet.max_row
#循环三次,移动了3列
j = 1
while j<4:
# 读取需求列
df = pd.read_excel(filename,usecols=[4],names=None)
df_li = df.values.tolist()
result = []
for s_li in df_li:
result.append(s_li[0])
#在第17列之前插入列,目标列
ws.insert_cols(17)
b=[]
i=0
while i < a:
i = i+1
b.insert(i, ws.cell (i,5).value)
i = 0
while i < a:
ws.cell (i+1,17).value = b[i]
i = i+1
#删除第5列,因为已经移动到第17列,把原数据删除
ws.delete_cols(5)
wb.save(filename)
j = j + 1
#心理暗示
print("It works!")