Я хочу скопировать фрейм данных из файла .csv и вставить его в существующий файл .xls на указанном листе и в указанном месте.
Этот код записывает фрейм данных в существующий файл Excel, но полностью перезаписывает файл. Рабочая книга также состоит из других вкладок.
результат был скопирован и вставлен в указанное место,
но другие вкладки и значения ячеек также были уничтожены.
import os
import pandas as pd
import datetime
import csv
import xlsxwriter
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.chart import LineChart,Reference
from win32com.client import Dispatch
import os
from pandas import ExcelWriter
xl = Dispatch("Excel.Application")
xl.Visible = True
#record start time
currentDT_start =datetime.datetime.now()
# Read index data from .csv
index_file=r"file\name\.."
analysis_file=r"file\name.."
py_index_file=index_file.replace('\\','\\\\')
py_analysis_file=analysis_file.replace('\\','\\\\')
#copying
index = pd.read_csv(py_index_file,skiprows=1)
#print(index.head())
#this code write the dataframe to the existing excel file, but it overwrite the file completely. the workbook consists of other tabs too.
# the outcome was it copied and pasted to specified location
# but other tabs and cells values got wiped out too.
writer=ExcelWriter(py_analysis_file)
index.to_excel(writer,'Indexes',startrow=1)
writer.save()
currentDT_end=datetime.datetime.now()
print('Code started on ' + currentDT_start.strftime("%Y-%m-%d %H:%M:%S") + ' !')
print('Code completed on ' + currentDT_end.strftime("%Y-%m-%d %H:%M:%S") + ' !')