upload an excel sheet to be added to the system

713 views
Skip to first unread message

Neveen Adel

unread,
Feb 2, 2011, 8:30:26 AM2/2/11
to web2py-users
Hello,

Is there a library that can be used to import an excel file that
insert records in a specific table ?

If there is no a library how can i do that ?

Thanks in Advance

Kenneth Lundström

unread,
Feb 2, 2011, 8:39:03 AM2/2/11
to web...@googlegroups.com
I know you can import/export CSV-files and export XLS files. But I don�t
know if there is a way to import XLS files directly.


Kenneth

Neveen Adel

unread,
Feb 2, 2011, 8:49:28 AM2/2/11
to web2py-users
Hello Kenneth,

Thanks for your reply:)

It is ok about import/export CSV files, How can i do this into my
application ?

On Feb 2, 3:39 pm, Kenneth Lundström <kenneth.t.lundst...@gmail.com>
wrote:

Bruno Rocha

unread,
Feb 2, 2011, 9:58:50 AM2/2/11
to web...@googlegroups.com

mart

unread,
Feb 2, 2011, 11:27:06 AM2/2/11
to web2py-users
sticking purely to text, is there a difference between an excel file
and csv? if formatted properly, it should be the same thing. Extension
names are only incidental, if web2py has the extension hard coded in
its look up, then you should be able to simply rename the file...

Neveen Adel

unread,
Feb 2, 2011, 8:05:35 PM2/2/11
to web2py-users
Thanks a lot Bruno Rocha for ur reply :)

But these packages are used after uploading the file.

I want to know the step before that, what i want to do simply is to
have a browse button to choose CSV file and then press upload then i
can by using one of the packages u suggested to read the CSV file.

Do you have any idea how can i do that?

Thanks in Advance

Brian M

unread,
Feb 2, 2011, 9:29:43 PM2/2/11
to web...@googlegroups.com
Neveen,

I've got a mini app that I upload excel files to for later parsing, here's now I do it. Getting the file would probably be even easier if you used an upload field in the DB & CRUD forms.

def excel_uploader():
    response.subtitle = "Upload excel file "
    from gluon.sqlhtml import form_factory
    form=form_factory(SQLField('import_xls','upload'))
    if form.accepts(request.vars,session):
        request.flash='Received: %s'%request.vars.import_xls
        path = os.path.join(request.folder,'private','spreadsheet_to_process.xls')
        #I happen to be copying the uploaded file to a known location & filename, but you wouldn't have to
        #the uploaded file gets saved to the /uploads folder with a auto-generated name too
        import shutil
        shutil.copyfileobj(request.vars.import_xls.file,open(path, 'wb'))

        #Then redirect to the next screen (or do the processing now)
        redirect(URL(r=request, f='process_excel'))

    return dict(form=form)

def process_excel():
    import xlrd
    from applications.app_name.modules.readexcel import readexcel as readexcel


    excel_filepath = os.path.join(request.folder,'private','spreadsheet_to_process.xls')
    xl = readexcel(excel_filepath)
    sheetnames = xl.worksheets()
    #for sheet in sheetnames:
    #    print sheet
    #for row in xl.getiter('Sheet  name'):
    #            print row
    return dict(rows = xl.getiter('Sheet Contents'))

Neveen Adel

unread,
Feb 3, 2011, 4:51:47 PM2/3/11
to web2py-users
Dear Brian,

Thanks a lot for your help.
>     #seehttp://code.activestate.com/recipes/483742-easy-cross-platform-excel-...

AbortedF

unread,
Mar 17, 2011, 10:34:40 AM3/17/11
to web2py-users
Microsoft Excel .xls format (as well as the others) saves its files
out in custom binary formats. It is very difficult to import them
unless you have a way of decoding the binary format.
Using csv, you can easily set up auto-importers (as long as the csv
data-structure is fairly static).

This is a bit hacky, but you should be able to get the idea...
########### CSV Example
def importer():
my_inserts=[]
form=SQLFORM.factory(
Field('csv_file', 'upload' ,
uploadfolder=os.path.join(request.folder,'static/temp'))
)
if form.accepts(request.vars, session):
my_file=open(os.path.join(request.folder, 'static/temp',
'form.vars.csv_file), 'r')
line=my_file.readline()
while line !="":
# Be careful how you delimit / split your vars!
my_vars=line.split(",") # or whatever
try:
my_inserts.append(
db.my_table.insert(
field_one=my_vars[0]
)
)
except:
# Error handling...
pass
my_file.close()
## Delete the file...
os.unlink(os.path.join(request.folder, 'static/temp',
form.vars.csv_file))
return dict(form=form, inserts=my_inserts)
###############################################################

Obviously it would be beneficial to do some validation etc. before
hand. There's almost definitely some decent python-based csv-parsing
module out there (and a better way to accomplish this task), but with
the way that the csv files (which I'm importing) are generated, I have
to manually parse them due to poor csv-exporting facilities in the
responsible apps.

Martin

kesh

unread,
Jul 26, 2017, 5:30:11 AM7/26/17
to web2py-users
how can i import excel row into a web2py sqlite table

Dave S

unread,
Jul 26, 2017, 5:46:35 AM7/26/17
to web2py-users


On Wednesday, July 26, 2017 at 2:30:11 AM UTC-7, kesh wrote:
how can i import excel row into a web2py sqlite table


If the excel data is in CSV format, you can import that directly.

/dps

Anthony

unread,
Jul 26, 2017, 10:26:36 AM7/26/17
to web2py-users
Note, if you have a new question, it is better to simply create a new topic rather than adding to a thread that is several years old.
Reply all
Reply to author
Forward
0 new messages