Python不会关闭excel工作簿
运行代码后,我似乎无法打开生成的excel文件,因为我的电脑说它是“在另一个程序中打开”。如果我关闭python,那么我可以打开它。
当我尝试将“workbook.close()”添加到代码中时,出现错误“在已关闭的文件上调用 close()”。所以看起来python已经关闭了它。
我该怎么做才能在不必关闭 python 的情况下看到我的输出?
import pandas as pd
# Import data
df = pd.read_excel (r'C:UsersDesktopworkchartsconstraints.xlsx' , sheet_name='biggestobstacle')
print (df)
# File 2 is the new excel table with the chart. Hence, we keep the originals untouched. We create a new excel + chart.
writer = pd.ExcelWriter("file2.xlsx", engine="xlsxwriter")
df.to_excel(writer, sheet_name='biggestobstacle')
# Get xlsxwriter objects
workbook = writer.book
worksheet = writer.sheets['biggestobstacle']
# Create a 'column' chart
chart = workbook.add_chart({'type': 'column'})
# select the values of the series and set a name for the series
chart.add_series({
'values': '='biggestobstacle'!$B$2:$B$8',
"name": "My Series's Name"
})
# Insert the chart into the worksheet in the D2 cell
worksheet.insert_chart('D2', chart)
writer.save()
# This last line generates an error.
workbook.close()