简体   繁体   中英

Excel Copy data without Formulas openpyxl

I'm trying copy and paste some data from one sheet to another sheet. The code works fine but I only need the value.

original_wb = xl.load_workbook(filename1)
copy_to_wb = xl.load_workbook(filename1)
source_sheet = original_wb.worksheets[0] # The first worksheet
copy_to_sheet = copy_to_wb.create_sheet(source_sheet.title+"_copy")
for row in source_sheet:
  for cell in row:
    copy_to_sheet[cell.coordinate].value = cell.value
copy_to_wb.save(str(filename1))

Can this be done in pandas instead?

if you want just values to be read and copied to new sheet . try read excel and write excel commands.

file_name= r"path"

#Read 
df= (pd.read_excel(io=file_name,sheet_name='name'))

#process required data

#write to new work book or sheet
df.to_excel( file_name ,sheet_name= 'name')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM