我正在尝试制作一个 Web API 来将 csv 保存到模板 excel 文件中。我制作了一个功能,当我执行它时它可以完美运行。但是一旦我从函数 (def on_get(self, req, resp): ) 调用它,文件就会损坏。我可以进行哪些更改以防止文件损坏?
服务器代码:
class Test:
def on_get(self, req, resp):
ct.save_df_into_excel(df_template, file_location, list_of_code_columns, new_location)
"""Handles GET requests"""
response = {
'status': 'success'
}
resp.media = response
功能:
def save_df_into_excel(df_template, file_location, list_of_code_columns, new_location = None):
if new_location == None:
new_location = file_location
if df_template.columns.nlevels > 1:
df_template.columns = df_template.columns.droplevel()
df_new_template = df_template.reindex(columns=list_of_code_columns)
workbook = openpyxl.load_workbook(file_location)
writer = pd.ExcelWriter(file_location, engine='openpyxl')
writer.book = workbook
writer.sheets = dict((ws.title, ws) for ws in workbook.worksheets)
worksheet = workbook.active
df_new_template.to_excel(writer, sheet_name=worksheet.title,
header=None, startrow=2, index=False)
writer.book.save(new_location)