Upload contents of Excel worksheet to Google Sheet worksheet - python

I have an Excel sheet with a worksheet named test_sheet. I have a Google sheet named G_sheet having 5 tabs. I would like to automate the process of copying the contents of Excel worksheet test_sheet and paste it in a specific tab, say 'sheet1', of the Google sheet. How can be done this using excel VBA?
I am using Excel 2013.

Your going to need
Pandas
Watchdog
Pygsheets
Pandas read xlsx/csv files, convert them to dictionaries, so that you can apply a batch update using pygsheets.
Watchdog, to watch you xlsx files for modifications. When a modification is made, you re-read the files with pandas, and update the spreadsheet on google using pygsheets.

I may be misinterpreting the question, but if you just want to copy a file from Excel to Google Sheets you can just upload it with their web interface and it will be automatically converted.
If you need the file to be synchronized, you can use the Google Drive sync client, or save the Excel sheet to OneDrive and use Zapier to synchronize the file.
If, for whatever reason, you need to upload the file programmatically, it will be easier to just automate the user inputs with a program like AutoHotkey than to create a Python program to do this.
If you really need the program in Python (which is after all one of the most useful languages for data processing), you can use xlwings to read the sheet and the Google Sheets API for output.
Another thing you could do to speed up the file read-in would be to add some simple VBA code to the Excel file to output the entire contents to a format more easily readable by python, or exporting the whole sheet as csv and then using the python csv library to read it in.
Another question is why you need it in Google Sheets format anyway. If you just need the file to be accessible via a link, you could upload the sheet to OneDrive and make it accessible. If you need it to be accessible by other programs over the internet, you could transfer it directly or make the file accessible over FTP.

If you have Google's OAuth2 credentials,:
http://gspread.readthedocs.io/en/latest/oauth2.html
just use:
https://github.com/burnash/gspread

You could use googles program called Backup and Sync to easily save any folder on your computer to your google drive.
You just have to set up the sync settings to where it only saves that one excel sheet to a place on your computer. And then every time you save the doc it will automatically be synced with the copy of the excel sheet on your google drive.

Related

Excel COM API for reading the cell index using Python

I am wiritng a Python script using Excel COM APIs. I wanted to search for a string in a excell cell ,once found I want to return the cell index(like A2 or B3).
what is the API I have to use for this..
Thx,
Jose
If you deal with open XML documents in Excel you may consider using the Open XML SDK for such tasks, see Welcome to the Open XML SDK 2.5 for Office for more information. You can try to search for any Python packages that provides the same functionality. See Extracting Data from Excel Files for more information.
Also you may consider automating Excel from Python. Read more about that in the Read only Excel Cells with values python win32com thread which also provides a sample code for that.

Interfacing Docker Container form Excel

I have a python code for doing "advanced analytics". Right now python reads input from an excel file (multiple sheets) and stores output to a new excel file.
Since python uses an external solver and custom modules I'm planning on putting everything in a docker container for convenience.
Now I'd like to be able to "click a button" in an excel workbook to send the contents of multiple sheets to my analytics engine and get the result back to a new sheet. I'm thinking about communicating with the docker container using API, but not sure if this is the most efficient approach. Is there a better way of solving this problem?

Modifying and writing data in an existing excel file using Python

I have an Excel file(xlsx) that already has lots of data in it. Now I am trying to use Python to write new data into this Excel file. I looked at xlwt, xldd, xlutils, and openpyxl, all of these modules requires you to load the data of my excel sheet, then apply changes and save to a new Excel file. Is there any way to just change the data in the existing excel sheet rather than load workbook or saving to new files?
This is not possible because XLSX files are zip archives and cannot be modified in place. In theory it might be possible to edit only a part of the archive that makes up an OOXML package but, in practice this is almost impossible because relevant data may be spread across different files.
Please check Openpyxl once again. You can load the data, do things with python, write your results in a new sheet in the same file or same sheet and save it (as everything is happening in memory).
e.g:
load data
wb = openpyxl.load_workbook("file.xlsx", data_only=True)
manipulate with python
# python codes
create sheet
some_sheet = wb.create_sheet("someSheet") # by default at the end
program to write in sheet
# program to write in sheet
save file (don't forget to close the excel file if its open before saving, as it will raise "Permission Error")
wb.save("file.xlsx"
here is the link
https://openpyxl.readthedocs.io/en/default/tutorial.html

Recording unsaved Excel data with python or automsaving with vba

Using python, I want to continuously record some Excel calculations. The python/excel functions I have used will only read excel data from a saved spreadsheet. Is there a way to read unsaved data?
If not, is there a way to save excel and read the saved data periodically? I tried saving my sheets periodically (with a macro) but this is problematic if I am interacting with the spreadsheet during a save. Instead of saving my spreadsheet, excel saves a copy with a random name. If there is a way to remedy this (maybe some kind of vba error handling) that may solve the problem. Thanks.
Assuming you are using Excel on Windows and not OS X, you can try the win32com.client module, which you can get by installing the Python for Windows package:
http://sourceforge.net/projects/pywin32/
Unfortunately, the available documentation is pretty spotty at best... here's a start:
http://docs.activestate.com/activepython/3.3/pywin32/html/com/win32com/HTML/QuickStartClientCom.html
I should warn you that the COM API isn't very easy to use, so you are really better off sticking with VBA if your workflow requires Excel.
VBA error handling: http://www.cpearson.com/excel/errorhandling.htm

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!

Categories