xlwings 新建工作表
时间: 2023-07-29 10:12:25 浏览: 242
使用 xlwings 在 Excel 中新建工作表非常方便,可以使用以下代码:
```python
import xlwings as xw
# 打开 Excel 应用程序
app = xw.App()
# 新建工作簿
wb = app.books.add()
# 新建工作表
sheet = wb.sheets.add('Sheet1')
# 保存工作簿
wb.save('example.xlsx')
# 关闭工作簿和 Excel 应用程序
wb.close()
app.quit()
```
在这个例子中,首先我们创建了一个 xlwings 的应用程序实例,然后新建了一个工作簿,并在其中新建了一个名为“Sheet1”的工作表。最后,我们保存了工作簿,关闭了工作簿和 Excel 应用程序。
相关问题
xlwings 新建工作薄 怎么选中sheet1
你可以使用 xlwings 的 activate 方法来激活 sheet1。例如:
```python
import xlwings as xw
app = xw.App()
workbook = app.books.add()
sheet1 = workbook.sheets[0]
sheet1.activate()
```
这将激活 sheet1 并使其成为活动工作表。
使用xlwings在路径path1下新建工作薄,然后新建一个sheetname="a"的工作表,然后将dataframe写入这个工作表
下面是使用xlwings在路径path1下新建工作薄,然后新建一个sheetname="a"的工作表,然后将dataframe写入这个工作表的代码:
```python
import xlwings as xw
import pandas as pd
# 创建Excel应用程序
app = xw.App(visible=False, add_book=False)
# 新建工作薄
wb = xw.Book()
# 获取第一个工作表
sheet = wb.sheets[0]
# 将dataframe写入a工作表
df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
sheet['a'].options(index=False, header=False).value = df
# 保存工作薄
wb.save(path1)
# 关闭工作薄和应用程序
wb.close()
app.quit()
```
阅读全文
相关推荐
















