'Copying range from one excel to another
I am trying to copy a range form one excel sheet to another. This is my code:
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')
But I get this error:
C_72_00_a = workbook_skema_72["C_72_00_a"]['D9':'F55']
TypeError: string indices must be integers
Am I defining the ranges in a wrong way or something? I hope you can point me in the right direction.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
