【问题标题】:Copying range from one excel workbook to another将范围从一个 Excel 工作簿复制到另一个
【发布时间】:2022-11-14 19:48:54
【问题描述】:

我正在尝试将一个范围从一个 excel 表复制到另一个。 这是我的代码:

import openpyxl
import os

#Current path
path = os.path.dirname(os.path.abspath(__file__))

#Beregningsmodul navn
Beregningsmodul_moder = "Beregning COREP LCR - MODER - 202202.xlsx"

#Skema 72 navn
workbook_skema_72 ="C_72_00_a.xlsx"

#workbook_beregn path
workbook_beregn_path = path + "\\" + Beregningsmodul_moder
workbook_beregn = openpyxl.load_workbook(workbook_beregn_path)

#Kopier til
wb_72C = workbook_beregn["72C"]['E8':'G54']

#kopier fra
C_72_00_a = workbook_skema_72["C_72_00_a"]['D9':'F55']

#Pair the rows
for row1,row2 in zip(C_72_00_a, workbook_beregn):
    #within the row pair, pair the cells
    for cell1, cell2 in zip(row1,row2):
        #assign the value of cell 1 to the destination cell 2 for each row
        cell2.value = cell1.value
#save document
workbook_beregn.save('destination.xlsx')

但我得到这个错误:

C_72_00_a = workbook_skema_72["C_72_00_a"]['D9':'F55']
TypeError: string indices must be integers

我是否以错误的方式定义范围? 我希望你能指出我正确的方向。

【问题讨论】:

  • workbook_skema_72 只是文件名,而不是工作簿对象。

标签: python excel openpyxl


【解决方案1】:

我最好的解决方案是使用 VBA 代码来解决这个问题,我希望这可以帮助将来的人:

Sub HentData()

'Improves performance / stability
Call OptimizeCode_Begin

Dim Time As Variant

'Monitor duration of runtime
StartDateTime = Now

Dim tws As Worksheet
Dim Path As String
Dim Files As String
Dim wb As Workbook

Set twb = ThisWorkbook
Set tws = twb.Worksheets("Currentpath")
Path = tws.Range("Path_Skema").Value
Files = Dir(Path & "*.xlsx")

'----------------
While Files <> ""
'----------------

        'Skema 72
        '--------------------------
        If Files Like "*C_72*" Then
        '--------------------------
        
            'ClearContents sheet "72C"
            Windows("Beregning COREP LCR - MODER.xlsm").Activate
                Sheets("72C").Select
                ActiveSheet.AutoFilterMode = False
                Cells.Select
                Selection.Clear
                Range("A1").Select
            
            'Open workbook "C_72_00_a.xlsx"
            Set wb = Workbooks.Open(Path & "" & Files)
            
            'Copy content
            Windows(Files).Activate
                ActiveSheet.Select
                Cells.Select
                Selection.Copy
                
            'Paste content
            Windows("Beregning COREP LCR - MODER.xlsm").Activate
                Sheets("72C").Select
                Range("A1").Select
                Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
                , SkipBlanks:=False, Transpose:=False
                Application.CutCopyMode = False
                
                'Format content
                Cells.Select
                Cells.EntireColumn.AutoFit
                Range("A1").Select
            
            'Close - C_72_00_a.xlsx
            Windows(Files).Activate
                Application.DisplayAlerts = False
                ActiveWindow.Close SaveChanges:=False
                Application.DisplayAlerts = True
            
            'Checks file timestamp
            Windows("Beregning COREP LCR - MODER.xlsm").Activate
                Sheets("Opdater_Skema").Select
                Time = Format(FileDateTime(Path & "" & Files), "mm-dd-yyyy hh:mm")
                Range("Date_C_72_00_a").Value = Time
            
        End If
        
        'Skema 73
        '--------------------------
        If Files Like "*C_73*" Then
        '--------------------------
        
            'ClearContents sheet "73C"
            Windows("Beregning COREP LCR - MODER.xlsm").Activate
                Sheets("73C").Select
                ActiveSheet.AutoFilterMode = False
                Cells.Select
                Selection.Clear
                Range("A1").Select
            
            'Open workbook "C_73_00_a.xlsx"
            Set wb = Workbooks.Open(Path & "" & Files)
            
            'Copy content
            Windows(Files).Activate
                ActiveSheet.Select
                Cells.Select
                Selection.Copy
                
            'Paste content
            Windows("Beregning COREP LCR - MODER.xlsm").Activate
                Sheets("73C").Select
                Range("A1").Select
                Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
                , SkipBlanks:=False, Transpose:=False
                Application.CutCopyMode = False
                
                'Format content
                Cells.Select
                Cells.EntireColumn.AutoFit
                Range("A1").Select
            
            'Close - C_73_00_a.xlsx
            Windows(Files).Activate
                Application.DisplayAlerts = False
                ActiveWindow.Close SaveChanges:=False
                Application.DisplayAlerts = True
            
            'Checks file timestamp
            Windows("Beregning COREP LCR - MODER.xlsm").Activate
                Sheets("Opdater_Skema").Select
                Time = Format(FileDateTime(Path & "" & Files), "mm-dd-yyyy hh:mm")
                Range("Date_C_73_00_a").Value = Time
        
        End If
        
        'Skema 74
        '--------------------------
        If Files Like "*C_74*" Then
        '--------------------------
            'ClearContents sheet "74C"
                Windows("Beregning COREP LCR - MODER.xlsm").Activate
                    Sheets("74C").Select
                    ActiveSheet.AutoFilterMode = False
                    Cells.Select
                    Selection.Clear
                    Range("A1").Select
                
                'Open workbook "C_74_00_a.xlsx"
                Set wb = Workbooks.Open(Path & "" & Files)
                
                'Copy content
                Windows(Files).Activate
                    ActiveSheet.Select
                    Cells.Select
                    Selection.Copy
                    
                'Paste content
                Windows("Beregning COREP LCR - MODER.xlsm").Activate
                    Sheets("74C").Select
                    Range("A1").Select
                    Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
                    , SkipBlanks:=False, Transpose:=False
                    Application.CutCopyMode = False
                    
                    'Format content
                    Cells.Select
                    Cells.EntireColumn.AutoFit
                    Range("A1").Select
                
                'Close - C_74_00_a.xlsx
                Windows(Files).Activate
                    Application.DisplayAlerts = False
                    ActiveWindow.Close SaveChanges:=False
                    Application.DisplayAlerts = True
                
                'Checks file timestamp
                Windows("Beregning COREP LCR - MODER.xlsm").Activate
                    Sheets("Opdater_Skema").Select
                    Time = Format(FileDateTime(Path & "" & Files), "mm-dd-yyyy hh:mm")
                    Range("Date_C_74_00_a").Value = Time
        End If
        
        'Skema 76
        If Files Like "*C_76*" Then
        'ClearContents sheet "76C"
                Windows("Beregning COREP LCR - MODER.xlsm").Activate
                    Sheets("76C").Select
                    ActiveSheet.AutoFilterMode = False
                    Cells.Select
                    Selection.Clear
                    Range("A1").Select
                
                'Open workbook "C_76_00_a.xlsx"
                Set wb = Workbooks.Open(Path & "" & Files)
                
                'Copy content
                Windows(Files).Activate
                    ActiveSheet.Select
                    Cells.Select
                    Selection.Copy
                    
                'Paste content
                Windows("Beregning COREP LCR - MODER.xlsm").Activate
                    Sheets("76C").Select
                    Range("A1").Select
                    Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
                    , SkipBlanks:=False, Transpose:=False
                    Application.CutCopyMode = False
                    
                    'Format content
                    Cells.Select
                    Cells.EntireColumn.AutoFit
                    Range("A1").Select
                
                'Close - C_76_00_a.xlsx
                Windows(Files).Activate
                    Application.DisplayAlerts = False
                    ActiveWindow.Close SaveChanges:=False
                    Application.DisplayAlerts = True
                
                'Checks file timestamp
                Windows("Beregning COREP LCR - MODER.xlsm").Activate
                    Sheets("Opdater_Skema").Select
                    Time = Format(FileDateTime(Path & "" & Files), "mm-dd-yyyy hh:mm")
                    Range("Date_C_76_00_a").Value = Time
        End If
    
    'Set variable to next file
    Files = Dir()

Wend

'---------
Windows("Beregning COREP LCR - MODER.xlsm").Activate
    Sheets("Opdater_Skema").Select
    Range("Opd.HentData") = Now
    Range("Opd.HentData").Select

'Monitor duration of runtime
EndDateTime = Now

'Duration of current run
Duration = Format(EndDateTime - StartDateTime, "hh:mm:ss")
Range("var_hentdata").Value = Duration

'ActiveWorkbook.Save

'Improves performance/stability
Call OptimizeCode_End

End Sub
Sub OptimizeCode_Begin()

Application.ScreenUpdating = False
ActiveSheet.DisplayPageBreaks = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.DisplayStatusBar = False
'Application.Calculation = xlCalculationManual

End Sub


Sub OptimizeCode_End()

Application.ScreenUpdating = True
ActiveSheet.DisplayPageBreaks = True
Application.EnableEvents = True
Application.DisplayAlerts = True
Application.DisplayStatusBar = True
Application.Calculation = xlCalculationAutomatic

End Sub

我将此代码附加到excel中的一个按钮,然后将其他工作表复制到工作表中。

excel中的函数可以找到以下变量:

路径:=LEFT(CELL("filename";$B$1);FIND("[";CELL("filename";$B$1);1)-1)

Skema_path:

=SUBSTITUTE(B2;"Intradag likviditet";"LCR skema")

【讨论】:

    猜你喜欢
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    相关资源
    最近更新 更多