I have a peculiar situation lets do a background.
Background:
We have a script that was developed in house for testing enterprise servers. The script is written in Bash. The magic of the script is the dmidecode command. The script runs on Linux Mint and is deployed via PXE. The output of the script contains hardware information about the processor, memory, system board and chassis part numbers, etc. This information is always changing depending on the device the image is booted on. All this data is then grepped into a readable format that then manually gets added into an Excel spreadsheet. The Excel sheet is broken up like this Row 1: A1(Asset Number),B1(Qty."always 1"),C1(Manufacturer),D1(Model),E1(Part Number),F1(Serial Number), G1(RAM (Mb)),H1(# of CPUs),L1(CPU Speed(GHZ))
Example Data
Column Names:
Column Data:
Row 2: 6112, 1, HP, Proliant DL580 G2, 325134-001, USE247ML97, 1024, 2, 2.8
So each row represents an individual server, the columns contain the data that comes from our previously mentioned bash script.
Goal:
OK, so my goal is to have the output of the bash script automatically be appended to an existing Worksheet that is stored on a local CIFS File Server. Since script is initiated on Linux, Powershell is not a viable option.
I think Openpyxl can do the job but I have no idea the best practice to approach this. I know that I will not be able to modify an existing workbook. Given that, I was thinking about loading an existing document in memory and overwriting with the same file name. Also, all the tutorials I see for Openpyxl seem to have static data, I do not have this luxury. Is this even possible or just a subtle daydream?
For an excel sheet i would use XLSX parser to read the spreadsheet and XLSX writer to make a whole new spreadsheet based on what you have read. However, for what you appear to be doing. Would it not be easier to put STDIO into a CSV?
e.g,
echo $VAR , $VARB , $VARC >> ~/servers.csv
Related
We have a rather complicated Excel based VBA Tool that shall be replaced by a proper Database and Python based application step by step.
There will be time of the transition between were the not yet completely ready Python tool and the already existing VBA solution will coexist.
To allow interoperability the Python tool must be able to export the database values into the Excel VBA Tool keeping it intact. Meaning that not only all VBA codes have to work as expected but also Shapes, Special Formats etc, Checkboxes etc. have to work after the export.
Currently a simple:
from openpyxl import load_workbook
wb = load_workbook(r'Tool.xlsm', keep_vba=True)
# Write some data i.e. (not required to destroy the file)
wb["SomeSheet!SomeCell"] = "SomeValue"
wb.save(r"Tool_filled.xlsm")
will destroy the file, i.e. shapes won't work, checkboxes neither. (The resulting file is only 5 MB from originally 8 MB, showing that something went quite wrong).
Is there a way to only modify only the data of an ExcelSheet keeping everything else intact/untouched?
As far I know an Excel Sheet are only zipped .xml files. So it should be possible to edit only the related sheets? Correct?
Is there a more comfortable way as writing everything from scratch to only modify the data of an existing Excel file?
Note: The solution has to work in Linux, so simple remote Excel calls are not an option.
I have an excel add-in (morningstar) that allows me to request and download data by entering a function with some parameters in cell A1. I have a thousand values of parameters that I need to get the data for so I would want to automate this process. I can (sort of) do that with VBA but it takes forever, and crashes many times. I would like to do this in Python. The logic which I am doing in VBA is as follows:
Open the xlsx file (workbook)
Refresh Excel so to make sure the Add-in is loaded and data is downloaded
Loop through sheets and save as csv files
I tried opening the excel file with python using the subprocess package. But I cannot make it until the data is downloaded. I attached 2 photos of the download process: (i)when the function in cell A1 is executed, the cell display "Processing..." . It would take a few seconds to a few minutes depending on the size of the data. when done, the data looks like in the second photo.
So my questions are:
How to ask python to start excel and load the add-in?
Execute the fuction in cell A1, or at least refresh excel to make sure the function runs.
Instead of asking python to wait for an exact time, how do I ask python to check if the data is downloaded?
Ideally, (I think) doing this without opening excel would save a lot of time, but not a major issue.
I am using python package 'jira' for establishing connection with the jira. I basically use the information from excel file and create tickets automatically in JIRA based on the excel information. Sometime there might be changes in the excel information for the same ticket in which case I need to run the code manually. So I would like to know if it is possible to do this automatically whenever there is a change in the excel file.
I assume you are only interested in filing new tickets. i.e. adding new rows in excel sheet.
Two options:
Run your code in a forever loop with sleep.
Have your code run by a cron.
Now, you can maintain the hash(md5 or sha256) of your file, and write the hash in some file on host machine if you are not using a database.
Your code has to read from this file, and calculate fresh hash of that excel file. If they are not same, means something has changed in your file.
Now, you also need to maintain till what row you have created the jira tickets. You can write this information also in some file.
I would like to process a large data set of a mechanical testing device with Python. The software of this device only allows to export the data as an Excel file. Therefore, I use the xlrd package which works fine for small *.xlsx files.
The problem I have is, that when I want to open a common data set (3-5 MB) by
xlrd.open_workbook(path_wb)
the access time is about 30s to 60s. Is there any more effecitve and faster way to access Excel files?
You could access the file as a database via PyPyODBC instead, which may (or may not) be faster - you'd have to try it out and compare the results.
This method should work for both .xls and .xlsx files. Unfortunately, it comes with a couple of caveats:
As far as I am aware, this will only work on Windows machines, since you're relying on the Microsoft Jet database driver.
The Microsoft Jet database driver can be rather buggy, especially with dates.
It's not possible to create or modify Excel files (a note in the PyPyODBC exceltests.py file says: I have not been able to successfully create or modify Excel files.). Your question seems to indicate that you're only interested in reading files, though, so hopefully this will not be a problem.
I just figured out that it wasn't actually the problem with the access time but I created an object in the same step. Now, by creating the object separately everything works fast and nice.
I have a viewer I built using WXPython. The viewer is basically a browser (built on IE wx.lib.iewin) that loads the txt or htm files I have in a directory and then lets me move through the files sequentially. Instead of having to go to the directory to select the next file to view the viewer/browser has a next button that loads the next file in the queue.
I want to be able to add a new feature that allows me to highlight some text that is visible in the browser and then push a button and have that text passed into a cell in excel.
Lots of things are going to have to happen like I need to be able to find and start a new instance of excel. I need to be able to add a new worksheet and pass some values to populate cells on the worksheet based on the file I am looking at and then if I want to collect some data from the file I want to be able to highlight the data in the viewer and then press a button on the viewer and have the data passed to excel.
I think I am going to start with PyWin32 but I am wondering if there is something else I need but I don't know enough to look for it.
If someone knows of an example where text was piped from a Python application to excel under the users control I would appreciate a pointer in that direction. It is easy enough I think to do this going from the application to a file that gets created (but not displayed) but I am hoping to go from the browser to the excel file so that the user can evaluate their work in progress.
I'd recomend using one of the python excel modules like python-excel.
It works on any OS, and without any other Excel application installed.
http://www.python-excel.org/
Code would look somehting liek this to write to a new xls document, slightly different to open an existing.
import xlwt
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet 1')
# indexing is zero based, row then column
sheet.write(0,1,'test text')
wbk.save('test.xls')
Hopefully this can get you on the right path, then you'll be able to post more specific questions if you run into problems.
Note: Another option is openpyxl:
http://packages.python.org/openpyxl/tutorial.html
If you're using wxPython (which I assume you are due to the tag), you should look at XLSGrid: http://www.blog.pythonlibrary.org/2011/08/20/wxpython-new-widget-announced-xlsgrid/
If you just want to work with Excel, I would recommend xlwt or xlrd, although you can use PyWin32 to work with it too via COM: http://www.blog.pythonlibrary.org/2010/07/16/python-and-microsoft-office-using-pywin32/