Open BytesIO (xlsx) with xlrd - python

I'm working with Django and need to read the sheets and cells of an uploaded xlsx file. It should be possible with xlrd but because the file has to stay in memory and may not be saved to a location I'm not sure how to continue.
The start point in this case is a web page with an upload input and a submit button. When submitted the file is caught with request.FILES['xlsx_file'].file and send to a processing class that would have to extract all the important data for further processing.
The type of request.FILES['xlsx_file'].file is BytesIO and xlrd is not able to read that type because of no getitem methode.
After converting the BytesIO to StringIO the error messages seems to stay the same '_io.StringIO' object has no attribute '__getitem__'
file_enc = chardet.detect(xlsx_file.read(8))['encoding']
xlsx_file.seek(0)
sio = io.StringIO(xlsx_file.read().decode(encoding=file_enc, errors='replace'))
workbook = xlrd.open_workbook(file_contents=sio)

I'm moving my comment into an answer of it's own. It related to the example code (which includes decoding) given in the updated question:
Ok, thanks for your pointers. I downloaded xlrd and tested it locally. It seems the best way to go here is to pass it a string ie. open_workbook(file_contents=xlsx_file.read().decode(encoding=file_enc, errors='replace')). I misunderstood the docs, but I'm positive that file_contents= will work with a string.

Try xlrd.open_workbook(file_contents=request.FILES['xlsx_file'].read())

I had a similar problem but in my case I needed to unit test a Djano app with download by the user of an xls file.
The basic code using StringIO worked for me.
class myTest(TestCase):
def test_download(self):
response = self.client('...')
f = StringIO.StringIO(response.content)
book = xlrd.open_workbook(file_contents = f.getvalue() )
...
#unit-tests here

Related

How to use numpy-stl with file uploaded with Flask request

I am writing an app, which takes a STL file as input. I want to get volume of the stl object without saving the stl file and use the volume to calculate a quote and post it back to browser. Right now I am using numpy-stl package, but I am stuck on how to create a mesh object for numpy-stl from the file I get with request.files['file'].read(). Any help is appreciated.
mycode:
what I get for filedata
what I get for error
You can try the following code:
import io
filedata = request.files['file'].read()
data = io.BytesIO(filedata)
tmp_mesh = mesh.Mesh.from_file("tmp.stl", fh=data)
You can use tmp_mesh object to do you interesting operation
suggestion to add error handle on something not expected
if request.files not contain 'file' keys

Comtypes has no attribute '_Application'

I'm experiencing a weird bug in a file conversion script I wrote in python. I'm using the comtypes library to convert different types of files into pdfs and for some reason, I get module 'comtypes.gen.Excel' has no attribute '_Application' whenever I try to create a client object for an excel application. I can't seem to find anything online specific to this issue. The script was working fine about a month or two ago, so I'm confused as to why it isn't working anymore - the only thing I could think of was excel updating or something (if that would even matter). I have the office 2016 if that's relevant. If anyone has experienced this bug or has any ideas, help would be greatly appreciated. Here's the script, for reference:
import comtypes.client
excel = comtypes.client.CreateObject("Excel.Application") # exception here
excel.Visible = False
in_file = "INPUT_FILE"
out_file = "OUT_FILE"
f = excel.Workbooks.Open(in_file)
f.ExportAsFixedFormat(0, out_file, 1, 0)
f.Close()
excel.Close()
Instead of using comtypes.client
Use pywin32
import win32com.client
#Replace excel=comtype.client.blablabla to
excel=win32com.client.DispatchEx("Excel.Application")
Keep remaining things as it is.
Ref. https://github.com/shardulind/pdfconv_python/blob/master/convertor.py

How to read this kind of .cvs file (html style content) by Python2.7?

I am practicing github machine learning contest using Python. I start from other's submission, but stuck at the first step: use pandas to read CSV file:
import pandas as pd
import numpy as np
filename = './facies_vectors.csv'
training_data = pd.read_csv(filename)
print(set(training_data["Well Name"]))
[enter image description here][1]training_data.head()
This gave me the following error message:
pandas.io.common.CParserError: Error tokenizing data. C error: Expected 1 fields in line 104, saw 3
I could not understand that the .csv file describe itself as html DOCTYPE. Please help.
The representing segments of the csv data content are attached. Thanks
It turns out I download the csv file following the convention of regular web operation: right click and save as. The right way is open the item from github, and then open it from the github desktop. I got the tables now. But the way to work with html files from python is definite something I would learn more about. Thanks.

how write hyperlink to local picture into the cell in openpyxl?

I use Python 2.7.3
I need to write hyperlink to local picture into the cell by openpyxl library.
when I need add hyperlink to web site I write something like this:
from openpyxl import Workbook
wb = Workbook()
dest_filename = r'empty_book.xlsx'
ws = wb.worksheets[0]
ws.title = 'Name'
hyperlink to local picture
ws.cell('B1').hyperlink = ('http://pythonhosted.org/openpyxl/api.html')
hyperlink to local picture
ws.cell('B2').hyperlink = ('1.png') # It doesn't work!
wb.save(filename = dest_filename)
I have 3 question:
how we can write hyperlink like VBA's style function:
ActiveCell.FormulaR1C1 = _
"=HYPERLINK(""http://stackoverflow.com/questions/ask"",""site"")"
with hyherlink and her name
how we can write hyperlink to local image?
ws.cell('B2').hyperlink = ('1.png') # It doesn't work! And I don't now what to do )
Plese, help me )
Can we use unicode hyperlinks to image? for example when I use
ws.cell('B1').hyperlink =
(u'http://pythonhosted.org/openpyxl/api.html') It fail with error!
for example we have picture 'russian_language_name.png' and we
create hyperlink in exel without any problem. We click to the cell,
and then print
'=Hyperlink("http://stackoverflow.com/questions/ask";"site_by_russian_language")
save document, unzip him. Then we go to him directory to xl->worksheets->sheet1.xml
and we see the title
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
and then ...
row r="2" x14ac:dyDescent="0.25" spans="2:6">-<c r="B2" t="str" s="1"><f>HYPERLINK("http://stackoverflow.com/questions/ask","site_by_russian_language")</f><v>site_by_russian_language</v></c>
everything ok =) Exel supports unicode, but what about python's library openpyxl? It support the unicode in hyperlinks ?
As the files in the .xlsx file are XML files with UTF-8 encoding, Unicode hyperlinks are not a problem.
About Question 2, you need to include the full path of the file link, i think.
If you cannot access the file link in your Excel file, it's the security strategy of Excel that prohibits such actions.
I answered a similar question. Hope this helps.
Well, I could arrive at this. While there is no direct way to build a hyperlink, in your case we could do this way. I was able to build a hyperlink to an existing file using the below code.
wb=openpyxl.Workbook()
s = wb.get_sheet_by_name('Sheet')
s['B4'].value = '=HYPERLINK("C:\\Users\\Manoj.Waghmare\\Desktop\\script.txt", "newfile")'
s['B4'].style = 'Hyperlink'
wb.save('trial.xlsx')
By mentioning the style attribute as 'Hyperlink' is the key. All other code I have may not be of any much importance to you. style attribute would otherwise have a value of 'Normal' Strange thing is even without the style attribute, the hyperlink we working but just that it was lacking style! of course. Though strange, I have seen stranger things. Hope this helps.

Django/Python: Save an HTML table to Excel

I have an HTML table that I'd like to be able to export to an Excel file. I already have an option to export the table into an IQY file, but I'd prefer something that didn't allow the user to refresh the data via Excel. I just want a feature that takes a snapshot of the table at the time the user clicks the link/button.
I'd prefer it if the feature was a link/button on the HTML page that allows the user to save the query results displayed in the table. It would also be nice if the formatting from the HTML/CSS could be retained. Is there a way to do this at all? Or, something I can modify with the IQY?
I can try to provide more details if needed. Thanks in advance.
You can use the excellent xlwt module.
It is very easy to use, and creates files in xls format (Excel 2003).
Here is an (untested!) example of use for a Django view:
from django.http import HttpResponse
import xlwt
def excel_view(request):
normal_style = xlwt.easyxf("""
font:
name Verdana
""")
response = HttpResponse(mimetype='application/ms-excel')
wb = xlwt.Workbook()
ws0 = wb.add_sheet('Worksheet')
ws0.write(0, 0, "something", normal_style)
wb.save(response)
return response
Use CSV. There's a module in Python ("csv") to generate it, and excel can read it natively.
Excel support opening an HTML file containing a table as a spreadsheet (even with CSS formatting).
You basically have to serve that HTML content from a django view, with the content-type application/ms-excel as Roberto said.
Or if you feel adventurous, you could use something like Downloadify to prepare the file to be downloaded on the client side.

Categories