Modifying and writing data in an existing excel file using Python - 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

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

Upload contents of Excel worksheet to Google Sheet worksheet

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.

Saving XlsxWriter workbook more than once

I am writing software that manipulates Excel sheets. So far, I've been using xlrd and xlwt to do so, and everything works pretty well.
It opens a sheet (xlrd) and copies select columns to a new workbook (xlwt)
It then opens the newly created workbook to read data (xlrd) and does some math and formatting with the data (which couldn't be done if the file isn't saved once) - (xlwt saves once again)
However, I am now willing to add charts in my documents, and this function is not supported by xlwt. I have found that xlsxwriter does, but this adds other complications to my code: xlsxwriter only has xlsxwriter.close(), which saves AND closes the document.
Does anyone know if there's any workaround for this? Whenever I use xlsxwriter.close(), my workbook object containing the document I'm writing isn't usable anymore.
Fundamentally, there is no reason you need to read twice and save twice. For your current (no charts) process, you can just read the data you need using xlrd; then do all your processing; and write once with xlwt.
Following this workflow, it is a relatively simple matter to replace xlwt with XlsxWriter.

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.

Combine tab-separated value (TSV) files into an Excel 2007 (XLSX) spreadsheet

I need to combine several tab-separated value (TSV) files into an Excel 2007 (XLSX) spreadsheet, preferably using Python. There is not much cleverness needed in combining them - just copying each TSV file onto a separate sheet in Excel will do. Of course, the data needs to be split into columns and rows same as Excel does when I manually copy-paste the data into the UI.
I've had a look at the raw XML file Excel 2007 generates and it's huge and complex, so writing that from scratch doesn't seem realistic. Are there any libraries available for this?
Looks like xlwt may serve your needs -- you can read each TSV file with Python's standard library csv module (which DOES do tab-separated as well as comma-separated etc, don't worry!-) and use xlwt (maybe via this cheatsheet;-) to create an XLS file, make sheets in it, build each sheet from the data you read via csv, etc. Not sure about XLSX vs plain XLS support but maybe the XLS might be enough...?
The best python module for directly creating Excel files is xlwt, but it doesn't support XLSX.
As I see it, your options are:
If you only have "several", you could just do it by hand.
Use pythonwin to control Excel through COM. This requires you to run the code on a Windows machine with Excel 2007 installed.
Use python to do some preprocessing on the TSV to produce a format that will make step (1) easier. I'm not sure if Excel reads TSV, but it will certainly read CSV files directly.
Note that Excel 2007 will quite happily read "legacy" XLS files (those written by Excel 97-2003 and by xlwt). You need XLSX files because .....?
If you want to go with the defaults that Excel will choose when deciding whether each piece of your data is a number, a date, or some text, use pythonwin to drive Excel 2007. If the data is in a fixed layout such that other than a possible heading row, each column contains data that is all of one known type, consider using xlwt.
You may wish to approach xlwt via http://www.python-excel.org which contains an up-to-date tutorial for xlrd, xlwt, and xlutils.

Categories