Search code examples
pythonexcelxlwings

How to copy and paste a filtered table in Excel using xlwings


I'm trying to copy only the visible data of a filtered Excel table to another range. My code so far is as shown:

import xlwings as xw

wb = xw.Book('Test Report.xlsx')
sht=wb.sheets('Sheet1')
sht.range('Table1').api.SpecialCells(12).copy

This does the copy and I can see the dashed lines around the table however I have no clue how to paste this somewhere else.


Solution

  • Ok just figured to do it this way:

    import xlwings as xw
    
    wb = xw.Book('Test Report.xlsx')
    sht=wb.sheets('Sheet1')
    sht.range('Table1').api.SpecialCells(12).copy
    sht.range('P2').select()
    sht.api.paste
    sht.api.Application.CutCopyMode=0