简体   繁体   中英

Can I create a table in excel with xlwings with python?

I am working in a report in excel and python, the main sheet contain a table. But every time I update the report the table format disappear and the table is converted to range.
here is my code

excel_app = xw.App(visible=False)  
wb = excel_app.books.open(path_open)  
ws = wb.sheets('Full_table')    
ws.cells(1, 1).options(index=False, header=True).value = my_data  

Would be possible to update the report keeping the table instead of having a range all the time? Thank you very much

assuming your data is in a dataframe called my_Data_table

following code can help achieve your goal

import xlwings as xw
book = xw.Book()
ws = book.sheets.active
ws.range("A1").options(index=False).value = my_Data_table

paste your data to excel

tbl_range = ws.range("A1").expand('table')

create table in excel

ws.api.ListObjects.Add(1, ws.api.Range(tbl_range.address))

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