简体   繁体   English

我可以用 xlwings 和 python 在 excel 中创建一个表吗?

[英]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.我正在编写 excel 和 python 的报告,主工作表包含一个表格。 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假设您的数据位于名为 my_Data_table 的 dataframe 中

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将您的数据粘贴到 excel

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

create table in excel在 excel 中创建表

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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