xlwings:保存并关闭 [英] xlwings: Save and Close
问题描述
我正在尝试找出在写入后如何使用xlwings保存和关闭现有工作簿的方法:
I am trying to find out how to save and close to an existing workbook using xlwings after writing in it:
import xlwings as xw
list_of_values = [1, 2, 3]
workbook_path = 'abc.xlsx'
wb = xw.Book(workbook_path)
ws = wb.sheets['sheet1']
ws.range('E35').value = list_of_values
wb.save()
wb.close()
当我进入wb.save(workbook_path)
时,出现提示:
该位置已存在名为abc.xlsx的文件".
您要更换它吗?'
When I get to wb.save(workbook_path)
, there is a prompt stating:
'A file named abc.xlsx' already exists in this location.
Do you want to replace it?'
我想立即覆盖文件而不会出现提示.根据文档,wb.save()
应该自动覆盖(请参阅: https://docs.xlwings.org/en/v0.6.4/api.html ).我也尝试过wb.save(workbook_path)
,但仍然会出现弹出窗口.
I want to overwrite the file immediately without the prompt coming up. According to the docs, wb.save()
should automatically overwrite (see: https://docs.xlwings.org/en/v0.6.4/api.html). I have also tried wb.save(workbook_path)
but the pop-up still appears.
请对此提供任何帮助.
p.s. -我基本上是想将数据写入预先格式化的Excel工作表中.如果还有其他方法可以保留格式,我将很乐于尝试.我已经尝试过了,但是在if newCell
处抛出了错误:
p.s. - I am basically trying to write data into a pre-formatted excel sheet. If there are other ways that can preserve the formatting, I would be happy to try it. I have tried this but it throws an error at if newCell
: Easily write formatted Excel from Python: Start with Excel formatted, use it in Python, and regenerate Excel from Python
推荐答案
You may need to specify the full path when you specify the path to wb.save()
:
path
(str,默认为None)–工作簿的完整路径.
path
(str, default None) – Full path to the workbook.
它将保存文件并覆盖而不提示.从他们的文档中:
It will save the file and overwrite without prompting. From their docs:
>>> from xlwings import Workbook
>>> wb = Workbook()
>>> wb.save()
>>> wb.save(r'C:\path\to\new_file_name.xlsx')
这篇关于xlwings:保存并关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!