2017-09-14 73 views
0

我希望有人能提供一點幫助。我試圖從一個Excel工作簿,名爲停機提取數據,並創建「規範」相匹配的是線圈經歷線圈(產品)的數字的字典。我已經能夠完成這部分,它非常簡單。創建從一個Excel工作簿一本字典,鍵與另一個工作簿匹配,粘貼值

即跳閘我的一部分,是如何匹配使用不同的Excel工作簿的線圈數量,並在相應的「代碼」粘貼。

所以這是我到目前爲止有:

import openpyxl 
from collections import defaultdict 

DT = openpyxl.load_workbook('DownTime.xlsm') 
bl2 = DT.get_sheet_by_name('BL2') 
CS = openpyxl.load_workbook('CoilSummary.xlsm') 
line = CS.get_sheet_by_name('BL2') 
#opening needed workbooks with specific worksheets 


coil =[] 
rc = [] 
code = defaultdict(set) 
cnum = '' 
next_row = 2 
col = 32 

for row in range(2, bl2.max_row + 1): 
    coil = bl2['K' + str(row)].value 
    rc = bl2['D' + str(row)].value 
    code[coil].add(rc) 
    # Creating a dictionary that represents each coil with corresponding codes 

for key,value in code.items(): 
    cnum = line['B' + str(row)].value 
    if cnum == key: 
     line.write(next_row, col, value) 
     next_row+=1 
# Attempting to match coil numbers with dictionary and column B 
# if the key is present, paste the value in column AF  

CS.close() 
DT.close() 

字典的樣本輸出如下:

('M30434269': {106, 107, 173}, 'M30434270': {132, 424, 106, 173, 188}, 'M30434271': {194, 426, 202, 106, 173}}) 

只有有22,000項。

因此,要重申我想要完成的任務:

我想利用這個字典,我從工作簿中的停機時間做,在CoilSummary列匹配的鑰匙,如果鍵進入細胞匹配,糊將值存入表格末尾的空白單元格中。

實施例:

"CoilNum"  "Date"   "Shift" "info1" "info2" "Code" 
M30322386 03/03/2017 06:48:30 3  1052  1722 ' ' 
M30322390 03/03/2017 05:18:26 3  703  1662 ' ' 

我想與在字典中的項匹配「CoilNum」,並將值粘貼到「代碼」。

我希望我解釋說不夠好。非常感謝您對代碼的任何幫助,或指向網站以供參考。我只是不想手動輸入所有這些代碼!

謝謝!

回答

1

大量的研究和反覆試驗後,意外損壞的Excel文件,並得到普遍使用Python沮喪和excel,我想通了。以下是我有:

# -*- coding: utf-8 -*- 

# importing tools needed for the code to work 
import pyexcel as pe 
from collections import defaultdict 
import openpyxl as op 


coil ='' 
rc = {} 
code = defaultdict(list) 
next_row = 2 
col = 33 
cnum = [] 
temp = '' 



def write_data(code,cnum): 
    ''' Used to open a given sheet in a workbook. The code will then compare values 
collected from one column in a specific sheet referred to as "coils" and compares it to a dictionary where the key's are also "coils." 
    If the coil number matches, the code will then paste the values in a new workbook. From here the values can be copied by hand and pasted into the excel file of choice.''' 
    sheet = pe.get_sheet(file_name="CoilSummaryTesting.xlsx") 
    next_row = 2 
    lst = [] 

    while next_row <= len(cnum): 
     for key in code.keys(): 
      for step in cnum: 
       if str(step) == str(key): 
        for val in code.values(): 
         temp = val 
         lst.append(temp) 
         next_row+=1 


       if step!=key: 
        break 
     break 

    for item in lst: 
     sublist = (" ").join(str(item)) 
     sheet.row+= [sublist] 
    sheet.save_as("CoilSummaryTest.xlsx") 
    print("\nCoils Compared: ",next_row)    



def open_downtime(): 
    ''' Pull data from a second excel file to obtain the coil numbers with corresponding downtime codes''' 

    DT = op.load_workbook('DownTime.xlsm') 
    bl2 = DT.get_sheet_by_name('BL2') 
    n = 1 

    for row in bl2.iter_cols(min_col=11,max_col=11): 
     for colD in row: 
      code[colD.offset(row=1,column=0).value].append(colD.offset(row=1,column=-7).value 
     n+=1 
    print('\nNumber of rows in DownTime file: ',n) 
    return code 

def open_coil(): 
    '''Opens the first workbook and sheet to know how many rows are needed for coil comparision.''' 

    i = 1 
    CSR = op.load_workbook('CoilSummaryTesting.xlsx') 
    line_read = CSR.get_sheet_by_name('BL2') 


    for rows in line_read.iter_cols(min_col=2, max_col=2): 
     for col in rows: 

      cnum.append(col.offset(row=1,column=0).value) 
      i+=1 

    print('\nNumber of rows in CoilSummary file: ',i)  



    return write_data(open_downtime(),cnum) 


def main(): 
    sheet = open_coil() 



if __name__ == "__main__": 
    main() 

我理解這可能不是這段代碼的最短的版本,有可能是很多的方式來得到它直接貼到我的選擇的Excel文件,但我不能現在還沒有確定這一部分。

我做了什麼不同的是使用pyexcel。當將值粘貼到行或列時,這被證明是最簡單的。使用連接,我打破了生成的列表清單,以允許每個子列表插入到它自己的行中。我目前決定將生成的行保存到不同的Excel工作簿中,因爲在此次探索過程中持續破壞工作簿;然而,如果有人知道如何操作這段代碼來消除必須複製行以粘貼到所需工作簿的最後一步,請讓我知道。

相關問題