Search code examples
pythonexceldatabasecopycopy-paste

Python Excel Copy data without Formulas


I tried to copy and paste some data from 2 different excel workbook with Win32 but in the Target wb I always have the formulas of the Source. The code works but I only need the value.

wb0 = excel.Workbooks.Open(pathSource)
ws0 = wb0.Worksheets('wsSource')
wb2 = excel.Workbooks.Open(pathTarget)
ws2 = wb2.Worksheets('wsTarget')
ws2.Name = 'NB DATA'
ws0 = wb0.Worksheets('wsTarget')
ws0.Range(datacopied).Copy(ws2.Range(datacopied))
wb2.Save()

Solution

  • Looking back on your previous questions, I see you asked this before and it had more example code in the question.

    I think what you may be looking for is this:

    wb0 = excel.Workbooks.Open(pathSource)
    ws0 = wb0.Worksheets('wsSource')
    wb2 = excel.Workbooks.Open(pathTarget)
    ws2 = wb2.Worksheets('wsTarget')
    ws2.Name = 'NB DATA'
    ws0 = wb0.Worksheets('wsTarget')
    # ws0.Range(datacopied).Copy(ws2.Range(datacopied))
    ws2.Range(datacopied).Value = ws0.Range(datacopied).Value
    wb2.Save()
    

    Taking the value of the range, and not simply copying it's contents is what you want it sounds like.

    EDIT: Keep in mind, this is assuming your datacopied variable is some kind of string that represents an excel cell range. I assumed this based on your previous question.