Save open excel file using python - python

How do I save an open excel file using python= I currently read the excel workbook using XLRD but I need to save the excel file so any changes the user inputs are read.
I have done this using a VBA script from within excel which saves the workbook every x seconds, but this is not ideal.

How about using xlwt?
for Python 2 -- https://pypi.python.org/pypi/xlwt/0.7.4
for Python 3 -- https://pypi.python.org/pypi/xlwt3/0.1.2

It looks like XLRD is used for reading the data, not interfacing with excel. So no, unless you use a different library using python is not the best way to do this, what is wrong with the VBA script?

Related

Appending Data to an Excel file using Python without using openpyxl

I am going to create a Excel application and I want to append the data to that Excel file without changing it's style openpyxl is giving a normal Excel so I want a different module to load with the previous styles and to save it
Also if some one know some example please share it
use xlsx writer this will open file without change in formatting you can save it back. as i have used it to save multiple file from a formatted template excel
pip install xlsxwriter

Modifying, recalculating and and extracting results from Excel in Python

I would like to try and make a program which does the following, preferably in Python, but if it needs to be C# or other, that's OK.
Writes data to an excel spreadsheet
Makes Excel recalculate formulas etc. in the modified spreadsheet
Extracts the results back out
I have used things like openpyxl before, but this obviously can't do step 2. Is the a way of recalculating the spreadsheet without opening it in Excel? Any thoughts greatly appreciated.
Thanks,
Jack
You need some sort of UI automation with which you can control a UI application such as Excel. Excel probably exposes some COM interface that you should be able to use for what you need. Python has the PyWin32 library which you should install, after which you'll have the win32com module available.
See also:
Excel Python API
Automation Excel from Python
If you don't necessarily have to work with Excel specifically and just need to do spreadsheet using Python, you might want to look at http://manns.github.io/pyspread/.
you could you pandas for reading in the data, using python to recalculate and then write the new files.
For pandas it's something like:
#Import Excel file
xls = pd.ExcelFile('path_to_file' + '/' + 'file.xlsx')
xls.parse('nyc-condominium-dataset', index_col='property_id', na_values=['NA'])
so not difficult. Here the link to pandas.
Have fun!

Python : .csv doesn't open well in MS excel

I write a csv using python's unicodecsv module like this :
with open(self.FILENAME, 'wb') as csvfile:$
writer = unicodecsv.writer(csvfile, delimiter='|',quotechar='"')
write_func(writer)
However opening this file directly in excel causes problems. The data doesn't seem to be written correctly. I get missing columns in excel and records overflowing to the other rows.
It works fine in Libreoffice
Is there anything that should be taken care of while writing to csv if I have to use that file in excel ?
Use delimiter used by default in Excel.
I'd advised to use openpyxl (or similar) Python library to create xlsx files instead. Then it will work with both LibreOffice and MS Excel. Opening of CSVs on Excel is made hard, and subtle details change from version to version.

xlrd - append data to already opened workbook

I am trying to write a python program for appending live stock quotes from a csv file to an excel file (which is already open) using xlrd and xlwt.
The task is summarised below.
From my stock-broker's application, a csv file is continually being updated on my hard disk.
I wish to write a program which, when run, would append the new data from csv file to an excel file, which is kept open (I wonder whether it is possible to read & write an open file).
I wish to keep the file open because I will be having stock-charts in it.
Is it possible? If yes, how?
Not directly. xlutils can use xlrd and xlwt to copy a spreadsheet, and appending to a "to be written" worksheet is straightforward. I don't think reading the open spreadsheet is a problem -- but xlwt will not write to the open book/sheet.
You might write an Excel VBA macro to draw the graphs. In principle, I think a macro from a command workbook could close your stock workbook, invoke your python code to copy and update, open the new spreadsheet, and maybe run the macro to re-draw the graphs.
Another approach is to use matplotlib for the graphs. I'd think a sleep loop could wake up every n seconds, grab the new csv data, append it to your "big" csv data, and re-draw the graph. Taking this approach keeps you in python and should make things a lot easier, imho. Disclosure: my Python is better than my VBA.

Python - Reading a spreadsheet

What I need to know is, can I get Python to read a spreadsheet (preferably Microsoft Excel), then parse the information and input it into an equation?
It's for a horse-racing program, where the information for several horses will be in one excel spreadsheet, in different rows or columns. I need to know if I can run a calculation for each of those horses separately and then calculate a score for the given horse.
My suggestion is:
Save the Excel file as a csv comma separated value file, which is a plain text format and much easier to work with.
Use Python's built-in csv module to work with the data in csv format.
You can work with Excel files directly in Python (Excel 2003 format supported via the third party modules xlwt, xlrd) but this is much harder than working with CSV.
OpenPyXL ("A Python library to read/write Excel 2007 xlsx/xlsm files") has a very nice and Pythonic API.
Use xlrd package. It's on PyPI, so you can just easy_install xlrd
You can export the spreadsheet as a .csv and read it in as a text file, then process it. I have a niggling feeling there might even a CSV parsing python library.
AFAIK there isn't a .xls parser, although I might be wrong.
EDIT: I was wrong: http://www.python-excel.org/

Categories