本來我是想嘗試,選中某個(gè)多個(gè)單元格復(fù)制到同一個(gè)sheet的其他位置,找了很多資料沒有找到,目前只有這么一個(gè)辦法,如果有大佬看到,歡迎補(bǔ)充請(qǐng)教。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# encoding:utf-8 import pandas as pd import openpyxl xl.to_excel(r "E:\55\crms.xlsx" ) wk = openpyxl.load_workbook(r "E:\55\crms.xlsx" ) #加載已經(jīng)存在的excel wk_name = wk.sheetnames wk_sheet = wk[wk_name[ 0 ]] wk_sheet.cell(row = 2 ,column = 2 ,value = '大區(qū)' ) #在第二行,第二列下入“大區(qū)”數(shù)值 wk_sheet.cell(row = 2 ,column = 3 ,value = '小區(qū)' ) wk_sheet.cell(row = 2 ,column = 4 ,value = '店鋪編碼' ) wk_sheet.cell(row = 2 ,column = 5 ,value = '店鋪名稱' ) wk.save(r "E:\55\s.xlsx" ) |
補(bǔ)充知識(shí):【openpyxl】python中對(duì)Excel進(jìn)行寫入操作,寫入一列或者一行(從excel中讀出label和feature對(duì)應(yīng)格式方法以及插入一行或者一列方法實(shí)現(xiàn))
前言
最近在做expansion of datset,所以需要把擴(kuò)展的dataset寫入到excel中
我已經(jīng)矩陣運(yùn)算全部搞定,最終輸出的是兩個(gè)輸出 labels 和 features
自己整理為以下格式
1
2
3
4
5
6
7
8
9
10
11
|
label = [[ 0 ], [ 1 ], [ 2 ], [ 3 ] ] feature = [ [ 0.1 , 0.2 , 0.3 , 0.4 , 0.5 ], [ 0.11 , 0.21 , 0.31 , 0.41 , 0.51 ], [ 0.6 , 0.7 , 0.8 , 0.9 , 1.00 ], [ 1.1 , 1.2 , 1.3 , 1.4 , 1.5 ], ] |
解決方案
先是準(zhǔn)備用python帶的xlrd xlrd 等包來操作感覺真的不太行
換思路,用第三方包openpyxl來操作
pip install openpyxl
官方文檔在這里
https://openpyxl.readthedocs.io/en/stable/index.html
代碼
廢話不多說,show you my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# coding=utf-8 from openpyxl import Workbook import numpy as np wb = Workbook() ws = wb.create_sheet( "che" ) label = [[ 0 ], [ 1 ], [ 2 ], [ 3 ] ] feature = [ [ 0.1 , 0.2 , 0.3 , 0.4 , 0.5 ], [ 0.11 , 0.21 , 0.31 , 0.41 , 0.51 ], [ 0.6 , 0.7 , 0.8 , 0.9 , 1.00 ], [ 1.1 , 1.2 , 1.3 , 1.4 , 1.5 ], ] #這個(gè)地方之所以 變成numpy格式是因?yàn)樵诤芏鄷r(shí)候我們都是在numpy格式下計(jì)算的,模擬一下預(yù)處理 label = np.array(label) feature = np.array(feature) label_input = [] for l in range ( len (label)): label_input.append(label[l][ 0 ]) ws.append(label_input) for f in range ( len (feature[ 0 ])): ws.append(feature[:, f].tolist()) wb.save( "chehongshu.xlsx" ) |
結(jié)果生成一個(gè)excel,最后結(jié)果如下圖:
總結(jié)
openpyxl包用起來是真的方便,對(duì)于寫入,只需要建立一個(gè)LIST進(jìn)行append就好了,如果excel為空的那append就從第一行開始遞增操作,你也可以理解為一個(gè)ws.append()操作就相當(dāng)于寫入一行,如果excel為有數(shù)據(jù)的時(shí)候,那寫入操作從沒有數(shù)據(jù)的那一行開始寫入;這里也說一下本來想用Insert來著但是忽略了一個(gè)條件,就是insert有個(gè)前提條件就是For example to insert a row at 7 (before the existing row 7):,意思為插入之前你的數(shù)據(jù)的大小一定是比要插入的行數(shù)或者列數(shù)大的,也就是說插入只能插到里面,不能在邊緣插。
插入核心參考代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
for col in range ( len (label)): print col ws.insert_cols(col + 1 ) for index, row in enumerate (ws.rows): #print row if index = = 0 : #row[col+1].value = label[col][0] print "label" print label[col] else : print "feature" print feature[col][index - 1 ] #row[col+1].value = feature[col][index-1] |
讀取代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
def create_data_expansion(path, sheet): data_init = pd.read_excel(path, sheet) # print data_init data_df = pd.DataFrame(data_init) print data_df data_df_transponse = data_df.T label_expansion = np.array(data_df_transponse.index) label_expansion_l = [] for l in range ( len (label_expansion)): label_expansion_l.append([l]) feature_expansion = np.array(data_df_transponse) label_expansion = np.array(label_expansion_l) return label_expansion, feature_expansion if __name__ = = "__main__" : path_name = "excel_demo.xlsx" sheet_name = "11" label, feature = create_data_expansion(path_name, sheet_name) print label print feature |
結(jié)果:
以上這篇python3.7 openpyxl 在excel單元格中寫入數(shù)據(jù)實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/u012874140/article/details/102302546