2017-01-03 59 views
0

我需要將許多excel文件編譯到一個excel文件中,然後將編譯後的文件複製到現有的excel文件中(使用宏/ .xlsm)在某張紙上。Python pandas將結果數據框寫入xlsm而不會丟失宏

我解決了第一個問題(將多個excel文件編譯成一個excel文件)。結果數據框以.csv格式保存。結果文件看起來像這樣。 the resulted dataframe

直到這裏沒有問題。下一步,我正在努力尋找如何去做。

從結果數據框中,我希望將數據框「複製並粘貼」到相應標題的工作表「Source」中的宏(.xlsm)的現有excel文件中。現有的Excel文件看起來像這樣。 target excel file

從上面的圖片可以看出,我想跳過在列A中寫入任何數據,因爲此列中的單元格滿了公式。我想在現有的Excel文件中將列B中的結果數據框寫入Q列。但是,在寫入數據之前,我想刪除所有單元格中的所有現有數據(A列中的單元格除外)。

所以基本上我要做到以下幾點:

  1. 刪除單元中的所有值列B到Q列在 現有XLSM文件(在片「源代碼」)
  2. 寫在導致數據幀到b列的新值,直到Q列
  3. 保存Excel文件後面的同名不失宏觀

任何反饋將b非常感謝!謝謝!

問候,

阿諾德

+0

開始錄製宏,而這樣做你列出的操作。然後拿出它的結果代碼並進行處理。 – user3598756

+0

基本上,我從你的問題中瞭解到,你想在數據框中將列B中的值替換爲Q? 如果是這種情況,那麼您可以使用'df.drop()'並通過'df [] = ' –

+0

@ user3598756添加新列謝謝您的評論。但是,我不是一個非常精明的人,所以我不完全理解你的建議。似乎您建議手動處理複製和粘貼數據。雖然我試圖自動化我的編譯工作。儘管如此,謝謝你的建議! – arnold

回答

0

對不起有點晚回來更新我的問題。最後,我用openpyxl軟件包解決了我的問題。

因此,這裏是我的最終代碼:

import openpyxl 
import os 
import string 
import pandas as pd 
import numpy as np 

path = #folder directory 
target_file = #excel filename 
sheetname = #working sheet that you wish to work on with 

filename = os.path.join(path, target_file) 

wb = openpyxl.load_workbook(filename, keep_vba=True) 
sheet = wb.get_sheet_by_name(sheetname) 

# To Erase All Values within Selected Columns 
d = dict() 
for x, y in zip(range(1, 27), string.ascii_lowercase): 
    d[x] = y.upper() 

max_row = sheet.max_row 
max_col = sheet.max_column 

for row in range(max_row): 
    row += 1 
    if row == 1: continue 
    for col in range(max_col): 
     col += 1 
     if col == 1: continue 
     sheet['{}{}'.format(d[col], row)] = None 

# To Write Values to the Blank Worksheet 
path_dataframe = # folder directory to the csv file 
target_compiled = # csv filename 
filename_compiled = os.path.join(path_compiled, target_compiled) 

compiled = pd.read_csv(filename_compiled, low_memory=False, encoding = "ISO-8859-1") 

for row in range(len(compiled.index)): 
    row += 1 
    if row == 1: continue # I do not want to change the value in row 1 in excel file because they are headers 
    for col in range(max_col): 
     col += 1 
     if col == 1: continue # I do not want to change the values in column 1 in excel file since they contain formula 
     value = compiled.iloc[row-2][col-2] 
     if type(value) is str: value = value 
     elif type(value) is np.float64: value = float(value) 
     elif type(value) is np.int64: value = int(value) 
     sheet['{}{}'.format(d[col], row)] = value 

wb.save(filename) 
0

由於您的CSV導入到電子表格可以使用Excel VBA宏使用QueryTables考慮有Python的複製VBA與COM接口,Excel對象庫來處理。之前的所有宏代碼保持不變,因爲沒有被覆蓋,但是單元數據。 注意:下面假定您正在使用Excel for Windows。

使用win32com庫,Python幾乎可以複製VBA所做的任何事情。事實上,您將會知道VBA是Office應用程序中的附加參考,並且絕不會是本機內置對象,並且會執行相同的COM接口!在您的IDE中查看Tools\References中的第一個選定項目。

import pandas as pd 
import win32com.client as win32 

# ...same pandas code...  
macrofile = "C:\\Path\\To\\Macro\\Workbook.xlsm" 
strfile = "C:\\Path\\To\\CSV\\Output.csv" 
df.to_csv(strfile) 

try: 
    xl = win32.gencache.EnsureDispatch('Excel.Application') 
    wb = xl.Workbooks.Open(macrofile) 

    # DELETE PREVIOUS DATA 
    wb.Sheets("Source").Range("B:Q").EntireColumn.Delete() 

    # ADD QUERYTABLE (SPECIFYING DESTINATION CELL START) 
    qt = wb.Sheets("Source").QueryTables.Add(Connection="TEXT;" + strfile, 
              Destination=wb.Sheets(1).Cells(2, 2)) 
    qt.TextFileParseType = 1 
    qt.TextFileConsecutiveDelimiter = False 
    qt.TextFileTabDelimiter = False 
    qt.TextFileSemicolonDelimiter = False 
    qt.TextFileCommaDelimiter = True 
    qt.TextFileSpaceDelimiter = False 
    qt.Refresh(BackgroundQuery=False) 

    # REMOVE QUERYTABLE 
    for qt in wb.Sheets("Source").QueryTables: 
     qt.Delete() 

    # CLOSES WORKBOOK AND SAVES CHANGES 
    wb.Close(True) 

except Exception as e: 
    print(e) 

finally:  
    qt = None 
    wb = None 
    xl = None 

可替代地,創建在VBA一個新的宏(置於一個獨立的模塊中),並且具有的Python調用它,傳遞csv文件路徑作爲參數:

VBA

Public Sub ImportCSV(strfile As String) 
    Dim qt As QueryTable 

    ThisWorkbook.Sheets("Source").Range("B:Q").EntireColumn.Delete 

    ' ADD QUERYTABLE 
    With ThisWorkbook.Sheets("Source").QueryTables.Add(Connection:="TEXT;" & strfile, _ 
     Destination:=ThisWorkbook.Sheets(1).Cells(2, 2)) 
      .TextFileParseType = xlDelimited 
      .TextFileConsecutiveDelimiter = False 
      .TextFileTabDelimiter = False 
      .TextFileSemicolonDelimiter = False 
      .TextFileCommaDelimiter = True 
      .TextFileSpaceDelimiter = False 

      .Refresh BackgroundQuery:=False 
    End With 

    ' REMOVE QUERYTABLE 
    For Each qt In ThisWorkbook.Sheets(1).QueryTables 
     qt.Delete 
    Next qt 

    Set qt = Nothing 
End Sub 

Python

import pandas as pd 
import win32com.client as win32 

# ...same pandas code...  
macrofile = "C:\\Path\\To\\Macro\\Workbook.xlsm" 
strfile = "C:\\Path\\To\\CSV\\Output.csv" 
df.to_csv(strfile) 

try: 
    xl = win32.gencache.EnsureDispatch('Excel.Application') 

    wb = xl.Workbooks.Open(macrofile) 
    xl.Application.Run('ImportCSV', strfile) 

    wb.Close(True) 
    xl.Quit 

except Exception as e: 
    print(e) 

finally:  
    wb = None 
    xl = None 
+0

感謝您的解釋!一旦我得到結果,我會盡力回來!感謝您花時間和精力解決我的問題! – arnold

相關問題