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

时间:2020-04-10 19:47:59

标签: python-3.x excel openpyxl

我有一个应用程序“ 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”。我看到其他一些用户也遇到了同样的问题,但显然没有解决吗?

1 个答案:

答案 0 :(得分: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

为清楚起见,outFile.xlsx是一个已经存在并且正在保存的文件。

相关问题