def generate_doc(request): from lxml import html, cssselect from docx import Document page = html.parse('http://127.0.0.1:8000/') root = page.getroot() ta_divs = root.cssselect('table') document = Document() all_data = [] for ta in ta_divs: data_dict = {} data_dict['title'] = [th.text_content() for th in ta.cssselect('th')] data_dict['content'] = [td.text_content() for td in ta.cssselect('td')] all_data.append(data_dict) document.add_heading('Something like dictionary', 0) table = document.add_table(rows=1, cols=5) hdr_cells = table.rows[0].cells for i in range(5): hdr_cells[i].text = all_data[0]['title'][i] for i in range(0,len(all_data[0]['content']),5): row_cells = table.add_row().cells row_cells[0].text = all_data[0]['content'][0+i] row_cells[1].text = all_data[0]['content'][1+i] row_cells[2].text = all_data[0]['content'][2+i] row_cells[3].text = all_data[0]['content'][3+i] row_cells[4].text = all_data[0]['content'][4+i] document.save('static/docs/demo.docx') return JsonResponse({'message': 'success'})
def list(request): # Handle file upload if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): newdoc = Document(docfile = request.FILES['docfile']) newdoc.save() # Redirect to the document list after POST return HttpResponseRedirect(reverse('exdb.views.list')) else: form = DocumentForm() # A empty, unbound form # Load documents for the list page documents = Document.objects.all() # Render list page with the documents and the form return render_to_response( 'list.html', {'documents': documents, 'form': form}, context_instance=RequestContext(request) )