使用Openpyxl保存格式。需要打开并再次保存文件

问题描述 投票:0回答:1

我有一个应用程序“ program.exe”,其中有一个excel文件作为参数:

program.exe file.xlsx

“ file.xlsx”首先需要修改。特别是我需要通过一些计算来转储数据框:

wb = load_workbook(filename="file.xlsx")
ws = wb.active
list2d = df.values.tolist()
for r_idx, row in enumerate(list2d, 1):
    for c_idx, value in enumerate(row, 1):
        ws.cell(row=r_idx+start_row, column=c_idx, value=value)

wb.save("file.xlsx")

但是,除非我手动打开并保存文件,否则我的程序不接受“ file.xlsx”。我看到其他一些用户也遇到了同样的问题,但显然没有解决吗?

python-3.x excel openpyxl
1个回答
0
投票

扩展评论:

是,程序开始如下:

import openpyxl as xl 

wb = xl.Workbook()
ws = wb.active

[lots of code to create/format the worksheet]

并以此结尾:

f = '/home/chris/myDir/outFile.xlsx'
wb.save(f)
Popen(['localc', f]) # to open the file after saving, as there's no way to simply open the worksheet/book after creating with openpyxl--`Popen` exists within the `subprocess` package
© www.soinside.com 2019 - 2024. All rights reserved.