我已经咨询了很多论坛,我无法得到答案.我已经在我的Django应用程序中安装了文件上传,以便将数据保存到我的服务器中.但它不起作用.相反,它会引发MultiValueDictKeyError.我想问题是没有request.FILES(因为它在request.FILES中提到了一个错误),所以文件上传不起作用.这是我的views.py:
def list_files(request, phase_id):
phase = get_object_or_404(Phase, pk=int(phase_id))
if request.method == 'POST':
#form = DocumentForm(request.POST, request.FILES)
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'], phase = phase_id)
newdoc.save()
doc_to_save = request.FILES['docfile']
filename = doc_to_save._get_name()
fd = open(settings.MEDIA_URL+'documents/'+str(filename),'wb')
for chunk in doc_to_save.chunks():
fd.write(chunk)
fd.close()
return HttpResponseRedirect(reverse('list_files'))
else:
form = DocumentForm()
documents = Document.objects.filter(phase=phase_id)
return render_to_response('teams_test/list_files.html',{'documents': documents, 'form':form, 'phase':phase}, context_instance = RequestContext(request)
)
Run Code Online (Sandbox Code Playgroud)
forms.py中的文档格式:
class DocumentForm(forms.ModelForm):
docfile = forms.FileField(label='Select a file', help_text='max. 42 megabytes')
class Meta:
model = Document …Run Code Online (Sandbox Code Playgroud) 我写了一个脚本,它必须从一个文件夹中读取很多excel文件(大约10,000个).此脚本加载excel文件(其中一些有超过2,000行)并读取一列来计算行数(检查内容).如果行数不等于给定数字,则会将警告写入日志中.
当脚本读取超过1,000个excel文件时出现问题.然后当它抛出内存错误时,我不知道问题出在哪里.以前,该脚本读取两个包含14,000行的csv文件并将其存储在列表中.这些列表包含excel文件的标识符及其各自的行数.如果此行数不等于excel文件的行数,则会写入警告.阅读这些清单可能是个问题?
我正在使用openpyxl来加载工作簿,在打开下一个工作簿之前是否需要关闭它们?
这是我的代码:
# -*- coding: utf-8 -*-
import os
from openpyxl import Workbook
import glob
import time
import csv
from time import gmtime,strftime
from openpyxl import load_workbook
folder = ''
conditions = 0
a = 0
flight_error = 0
condition_error = 0
typical_flight_error = 0
SP_error = 0
cond_numbers = []
with open('Conditions.csv','rb') as csv_name: # Abre el fichero csv donde estarán las equivalencias
csv_read = csv.reader(csv_name,delimiter='\t')
for reads in csv_read:
cond_numbers.append(reads)
flight_TF = []
with open('vuelo-TF.csv','rb') as …Run Code Online (Sandbox Code Playgroud) 我在这里有一个关于 python、openpyxl 和 Excel 文件的大问题。我的目标是将一些计算数据写入 Excel 中的预配置模板。我加载这个模板并在上面写入数据。有两个问题:
我不知道解决这个问题的其他方法。也许 openpyxl 不是解决方案。我试过用xlsb写,但我认为openpyxl不支持这种格式。我也尝试过优化的写入器和读取器,但是由于大数据,当我保存时出现问题。但是,输出文件大小最多为 10 MB。我非常坚持这一点。你知道是否有另一种方法可以做到这一点?
提前致谢。
我通过AJAX调用从服务器获取事件列表.我将"可编辑"选项设置为"true",但它仅适用于agendaDay和agendaWeek视图,而不适用于月视图.为什么?这是代码:
$('#calendar').fullCalendar({
header: {
right: "agendaDay,agendaWeek,month prev,next"
},
firstDay: 1,
fixedWeekCount: false,
contentHeight: 700,
timeFormat: "HH:mm",
displayEventEnd: {
month: false,
agendaWeek: false,
agendaDay: false,
'default':true
},
axisFormat: "HH:mm",
slotEventOverlap: false,
editable: true,
eventSources: [
{ // 1st group: Miscellanea
events: function(start,end,timezone,callback){
callAjax("Miscellanea",callback);
},
color: "#086A87",
},{ // 2nd group: project init
events: function(start,end,timezone,callback){
callAjax("Project",callback);
},
color: "#B40404"
}
]
});
Run Code Online (Sandbox Code Playgroud)
这是我的函数调用Ajax:
function callAjax(type,callback){
$.ajax({
type: "GET",
url: "/projects/{{project.id}}/get_events/",
dataType: "json",
data: {"data":type},
success: function(response){
data = eval("(" + response + …Run Code Online (Sandbox Code Playgroud) 我一直win32com在开发服务器中使用模块来轻松地从转换xlsx为pdf:
o = win32com.client.Dispatch("Excel.Application")
o.Visible = False
o.DisplayAlerts = False
wb = o.Workbooks.Open("test.xlsx")))
wb.WorkSheets("sheet1").Select()
wb.ActiveSheet.ExportAsFixedFormat(0, "test.pdf")
o.Quit()
Run Code Online (Sandbox Code Playgroud)
但是,我已经Django在没有安装Excel应用程序的生产服务器中部署了我的应用程序,这会引发以下错误:
File "C:\virtualenvs\structuraldb\lib\site-packages\win32com\client\__init__.p
y", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,c
lsctx)
File "C:\virtualenvs\structuraldb\lib\site-packages\win32com\client\dynamic.py
", line 114, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\virtualenvs\structuraldb\lib\site-packages\win32com\client\dynamic.py
", line 91, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.II
D_IDispatch)
com_error: (-2147221005, 'Invalid class string', None, None)
Run Code Online (Sandbox Code Playgroud)
在Python中是否有很好的替代方法可以将转换xlsx为PDF?
我已经使用PDFWriter测试了xtopdf,但是使用此解决方案,您需要读取和迭代范围并逐行写入行。我想知道是否有类似于win32com.client的更直接的解决方案。
谢谢!