使用Pandas读取Python CGI脚本中上传的XLSX文件
我正在创建一个工具,其中使用Pandas读取Python CGI脚本中上传的XLSX文件,python,pandas,cgi,Python,Pandas,Cgi,我正在创建一个工具,其中 将生成一个新的XLSX文件供用户下载 用户可以上传他们拥有的XLSX文件,我将读取该文件的内容,并使用它们生成一个新文件供用户下载 我想利用Pandas将XLSX文件读入数据帧,这样我就可以轻松地使用它。然而,我不能让它工作。你能帮我吗 从CGI文件提取的示例: import pandas as pd import cgi from mako.template import Template from mako.lookup import TemplateLookup
import pandas as pd
import cgi
from mako.template import Template
from mako.lookup import TemplateLookup
import http.cookies as Cookie
import os
import tempfile
import shutil
import sys
cookie = Cookie.SimpleCookie(os.environ.get("HTTP_COOKIE"))
method = os.environ.get("REQUEST_METHOD", "GET")
templates = TemplateLookup(directories = ['templates'], output_encoding='utf-8')
if method == "GET": # This is for getting the page
template = templates.get_template("my.html")
sys.stdout.flush()
sys.stdout.buffer.write(b"Content-Type: text/html\n\n")
sys.stdout.buffer.write(
template.render())
if method == "POST":
form = cgi.FieldStorage()
print("Content-Type: application/vnd.ms-excel")
print("Content-Disposition: attachment; filename=NewFile.xlsx\n")
output_path = "/tmp/" + next(tempfile._get_candidate_names()) + '.xlsx'
data = *some pandas dataframe previously created*
if "editfile" in form:
myfilename = form['myfile'].filename
with open(myfilename, 'wb') as f:
f.write(form['myfile'].file.read())
data = pd.read_excel(myfilename)
data.to_excel(output_path)
with open(path, "rb") as f:
sys.stdout.flush()
shutil.copyfileobj(f, sys.stdout.buffer)
从HTML文件提取的示例:
<p>Press the button below to generate a new version of the xlsx file</p>
<form method=post>
<p><input type=submit value='Generate new version of file' name='newfile'>
<div class="wrapper">
</div>
</form>
<br>
<p>Or upload a file.</p>
<p>In this case, a new file will be created using the contents of this file.</p>
<form method="post" enctype="multipart/form-data">
<input id="fileupload" name="myfile" type="file" />
<input value="Upload and create new file" name='editfile' type="submit" />
</form>
按下面的按钮生成xlsx文件的新版本
或者上传一个文件
在这种情况下,将使用此文件的内容创建一个新文件
这在表单:位中没有if“editfile”的情况下工作,因此当我试图访问用户上传的文件时,我知道出现了问题
问题是,创建文件时,创建的文件大小为0 KB,无法在Excel中打开。关键是,在我写出来的位置找不到用户上传的文件。您已将myfilename
传递给熊猫;但是,该文件在服务器上还不存在。在使用该文件之前,必须先将其保存在本地某个位置
下面将把文件下载到当前目录(与CGI脚本相同的目录)。当然,根据您的设置,欢迎您将其保存到更合适的目录中
form = cgi.FieldStorage()
myfilename = form['myfile'].filename
with open(myfilename, 'wb') as f: # Save the file locally
f.write(form['myfile'].file.read())
data = pd.read_excel(myfilename)
谢谢你的解释。出于某种原因,这对我不起作用。运行此命令后,我无法在与CGI脚本相同的目录中看到该文件。我将扩展我的问题。也许我在原来的帖子中遗漏了一些相关的内容。对于你更新的问题,当你说有什么地方出了问题时,你能描述一下实际发生的情况并粘贴你得到的任何错误吗。这是一个内部服务器错误吗?如果是这样,最好检查日志,例如,如果您使用apache2,它将是这样的:tail-20/var/log/apache2/error.log
。谢谢@costaparas,我在问题的末尾添加了几句话来澄清。运行此命令时,错误日志中没有添加任何内容。然而,我在访问日志中看到一条408消息——我想是超时。我无法用当前代码完全重现这条消息。这正是你正在运行的吗?您可能只需要切换到XLS格式,因为您的版本可能不支持XLSX。请参阅详细信息。我已在脚本开头启用了“cgib”模块import cgib
,cgib.enable
。现在,XLSX文件输出的文件大小为23KB。当我在一个文本编辑器中打开它时,我可以看到一个错误日志,其中包含Python中的错误描述——如果您正在运行一个.py文件,就会看到这一点。这清楚地表明,错误与我可以修复的无关内容有关。我很高兴接受你的回答,它解决了这个具体问题。谢谢你的帮助@costaparas。