当我制作猎鹰API时,我的Excel被损坏了



我正在尝试制作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)

根据我的经验,与Falcon处理传入文件的最佳方法是使用Falcon-Multipart中间件,您可以在此处获取https://github.com/yohanbonbonbonef多部分并这样使用:

from falcon_multipart.middleware import MultipartMiddleware
api = falcon.API(middleware=[MultipartMiddleware()])

然后在您的终点中使用类似的东西:

class UploadHandler():
    def on_post(self, req, resp):
        iFile = req.get_param('sentFile')

相关内容

  • 没有找到相关文章

最新更新