Python Openpyxl: Kopieren und Einfügen von einer Excel-Arbeitsmappe in eine andere – InhaltPython

Python-Programme
Anonymous
 Python Openpyxl: Kopieren und Einfügen von einer Excel-Arbeitsmappe in eine andere – Inhalt

Post by Anonymous »

Dies ist das erste Mal, dass ich Openpyxl verwende. Ich versuche also, mithilfe der Bibliothek Inhalte aus einer Excel-Arbeitsmappe in eine andere zu kopieren und einzufügen. Der Code funktioniert einwandfrei, wenn ich ihn ausführe. Meine Herausforderung besteht jedoch darin, dass der Code nicht den Inhalt unter den bereits in der Arbeitsmappe enthaltenen Inhalt kopiert, sondern den Inhalt in der Arbeitsmappe ersetzt. Und das ist nicht mein Ziel.
Ich habe versucht, mir das Tutorial anzusehen und auch etwas zu recherchieren, aber ich konnte anscheinend nicht verstehen, was ich falsch gemacht habe. Unten ist der Code, den ich verwende

Code: Select all

import openpyxl as opxl

#Opening the destination excel file
sourceFile = "C:\\ForTesting\\sourceFile.xlsx"

sourceworkbook = opxl.load_workbook(sourceFile)
sourceworksheet = sourceworkbook.worksheets[0]

#Opening the destination excel file
destinationFile = "C:\\ForTesting\\destinationFile.xlsx"

desinationworkbook = opxl.load_workbook(destinationFile)
destinationworksheet = desinationworkbook.active

# calculate total number of rows and
# columns in source excel file
maximum_row = sourceworksheet.max_row
maximum_column = sourceworksheet.max_column

# copying the cell values from source
# excel file to destination excel file
for i in range (1, maximum_row + 1):
for j in range (1, maximum_column + 1):
#reading the cell value from source excel file
c = sourceworksheet.cell(row = i, column = j)

#writing the value read from the source file to destination file
destinationworksheet.cell(row = i, column = j).value = c.value

#Save the destination excel file
sourceworkbook.save(str(destinationFile))

print("The File has been saved successfully")
Ich würde mich freuen, wenn mir jemand zeigen kann, was ich falsch mache oder was ich tun muss.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post