I saved a Microsoft Word document as an HTML document. I then copied and pasted the generated HTML into a Django template "document.html".
I then mapped a URL to a simple view that loads this template.
def viewDocument(request):
return render_to_response('document.html')
Afterwards, I get this error when access the page for that view.
UnicodeDecodeError at /viewDocument/
'utf8' codec can't decode byte 0xd2 in position 67600: invalid continuation byte
Strangely, the HTML in document.html displays fine when I open the page generated from Microsoft Word in Google Chrome on my laptop. How come it doesn't work as a Django template?
Because the file isn't encoded in UTF-8.
Related
I have an html page that I want to convert to PDF in several copies but in the same document. But when I do this it's the last line of my file that generate then I wanted to have a PDF document so the number of pages is the size of my database.
I was have the same problem 2 months ago .. and after trying several libraries to handle this after all i realized that chrome browser is best tool to convert html to pdf >> so i made
1 - special view to return the original html.
2 - use chrome from cli to convert this page to pdf and save it like :
os.system( f'chromium --headless --disable-gpu --print-to-pdf=<location to save>.pdf --no-margins <your_view_url>')
3 - then go to this place and return it as normal file.
i have a view in my django project that should be able to read the content of an uploaded .txt file from the input type="file", but the thing is that with arabic content it doesn't print the actual text, but a series of codes "\xd9\x88\xd9\x82\xd8\xa7\xd9\x84" and i couldn't find any solution for this since the file is perfectly viewable on my pc and my website it the one exporting that file in "utf-8". any help here ?
Uploaded_File = request.FILES["Doc"]
for chunk in Uploaded_File.chunks(chunk_size=None):
print(chunk)
I'm trying to import a csv file to my django project. Until now, the previous times I did this i never had a problem. However, all of a sudden I keep getting this error that says "Imported file has a wrong encoding: 'charmap' codec can't decode byte 0x9d in position 21221: character maps to" when i try to import the csv file in.
I don't understand why I am getting the error
[![enter image description here][1]][1]
this is what i keep getting.
I am trying to import my excel file like this:
[![enter image description here][2]][2]
and this is what my csv file looks like:
[![enter image description here][3]][3]
\
The csv file contains invalid data for the encoding that you are attempting to interpret it with. Depending on how it was generated, you might be able to tell Python what is the correct decoding to apply when you open it
f = open(csv_file_name, encoding= ...)
or you might specify an appropriate encoding when you generate the csv file, or you might be processing dodgy data and have to resort to encoding="latin-1" -- which may result in putting bad data in to your database, if you don't validate what comes out of the csv file through a Django form before saving it.
I'd recommend always processing rows of csv data through Django forms or modelforms. It makes it very easy to catch errors (form is not valid, form.errors, etc. ) and print out useful error messages about what is wrong with which field (column) of the row.
Lots more here and here
Hex character 9d is not a printable character (https://www.codetable.net/hex/9d). In unicode it appears to be some kind of command. You will need to sanitise this character to be able to handle it in a csv file.
Edit: As #snakecharmerb points out in the comments, there are encodings where this is a valid character. However, I suspect from your question that you are not using one of these.
You can also look into decode to allow you to specify a charset for reading the data. If you have a charset that you think this is a valid char for, then perhaps your routine is picking up a different default charset from the system.
I actually do something like this to make sure I get Swedish chars properly set. This is directly from my code when extracting fields
output.decode('iso-8859-1').strip()
I am working with Django.
I have a HTML form which collects information from the user,wrote a function which post the value.But when I amtrying to render the value to an URL.(Example:return of the function is like:http://api.test.com?search=topic?title=USA).
But it shows UnicodeEncodeError with
'charmap' codec can't encode characters
I googled and try to solve from other solution found.Such as using external libraries.
Actually,I want to add user parameter within URL that will be shown in a text area.
How can I do that?
You should use Urllib.urlencode to create get parameters to be added to the url.
I guess, that should solve your problem
I have an application that loads different documents to the server, and allows users to read documents' content.
I am uploading the documents to the server, and then I try to read the courses by id, like:
def view_course(request,id):
u = Courses.objects.get(pk=id)
etc
But I don't find anywhere: how can I actually read the content of a /.doc/.pdf/.txt and display it on a web page?
Reading plain text files is trivial, while PDF and Word processing is not. For the latter two you'll have to incorporate some external libraries.
Text: f.read()
Word: extracting text from MS word files in python
PDF: http://www.unixuser.org/~euske/python/pdfminer/index.html