开发者

Cannot save files using xlwings

开发者 https://www.devze.com 2022-12-07 21:11 出处:网络
I am trying to autofit all the columns in an Excel xlsx file in Python 3, but am running into issues with the save() function. Below is the function I am using:

I am trying to autofit all the columns in an Excel xlsx file in Python 3, but am running into issues with the save() function. Below is the function I am using:

import xlwings as xw

def autofit(path): # path = 'C:\...\output.xlsx'

    with xw.App(visible=False) as app:
        wb = xw.Book(path)
        for ws in wb.sheets:
            ws.autofit(axis = "columns")
        wb.save()
        wb.close()

The autofit works as expected, but it does not look like the file is being saved. In particular, the outputted file does not have the columns autofitted in the directory that the path specifies. I also tried doing wb.save(path) but it does not work. However, if I try a different path within the same directory, then开发者_JAVA技巧 I get the desired autofitted output (e.g. path = 'C:...\output_2.xlsx' rather than path = 'C:...\output.xlsx'). Is there a way that I can get the original file saved without having to create another file with a different name?

Thanks!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号