4 Replies - 241 Views - Last Post: 22 August 2014 - 01:00 PM Rate Topic: -----

#1 jgrimmie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-August 14

How to open a spreed sheet with python.

Posted 22 August 2014 - 12:40 PM

I want to open a spreed sheet that my python program created when the program exits. So my python code builds a spreadsheet then saves it but I want it to be opened in excel as the last thing the program does when it has finished. I am having no luck finding anything to do this online as my searches are only returning how to see spreadsheet with in a python console window and I just want it to open the file in excel.

Think of it like automating data entry and then automating the act of opening the file in excel after the file has been created.

My current code isn't rely relevant to the question but ere it is anyways:

##crac unit reporting...
import xlwt
from datetime import datetime

class Unit():
	def __init__(self, name="unknown", temperature=0, humitty = 0):
		self.name = name
		self.temperature = temperature
		self.humitty = humitty
		
	def sys_health(self):
		if self.temperature > 85:
			#print self.temperature #for debug use
			print "Temperature is out of tolerance!"
		else:
			print "Temperature is within tolerance!"
		if self.humitty > 60:
			#print self.humitty #for debug use
			print "Humitty is out of tolerance!"
		else:
			print "Humitty is within tolerance!"
			
wb = xlwt.Workbook()
ws = wb.add_sheet("A test sheet") #this is to create the sheet but I need to change this to something relevant later

#This is the first Row/Column to be written to for the crac unit's ID.
unitIdRow = 1
unitIdColumn = 1

#This is the first Row/Column to be written to for the first units temperature.
unitTempRow = 3 
unitTempColumn = 1

#This is the first Row/Column to be written to for the first units humitty.
unitHumittyRow = 3
unitHumittyColumn = 2

#This is the first Row/Column to be written to for the Designation of temperature column's.
tempDesignationRow = 2
tempDesignationColumn = 1

#This is the first Row/Column to be written to for the Designation of humitty column's.
humittyDesignationRow = 2
humittyDesignationColumn = 2

active_units = ['crac-1', 'crac4', 'crac5', 'crac9', 'crac10', 'crac12', 'crac15', 'crac16', 'crac17', 'crac18', 'crac19', 'crac20', 'crac21', 'crac22', 'crac23', 'crac24', 'crac26']
#Need to have a way of altering this list 

for I in active_units: # This loop is for printing the headers that should not be reprinted.
	ws.write( tempDesignationRow, tempDesignationColumn, "Temp.")
	tempDesignationColumn = tempDesignationColumn + 2
	
	ws.write( humittyDesignationRow, humittyDesignationColumn, "Humitty")
	humittyDesignationColumn = humittyDesignationColumn + 2
	
	

for i in active_units: #This loop is for getting temp and humitty from the user and writing them to spreadsheet.
	print "__________________________________________________________"
	
	print "Current unit %s " % i
	ws.write( unitIdRow, unitIdColumn, i)
	unitIdColumn = unitIdColumn + 2
	
	currentUnitTemp = int(raw_input("Enter the current temperature for unit %s: " % i))
	ws.write( unitTempRow, unitTempColumn, currentUnitTemp)
	unitTempColumn = unitTempColumn + 2

	
	currentUnitHumitty = int(raw_input("Enter the current humitty for unit %s: " % i))
	ws.write( unitHumittyRow, unitHumittyColumn, currentUnitHumitty)
	unitHumittyColumn = unitHumittyColumn + 2

	
	i = Unit(i, currentUnitTemp, currentUnitHumitty)
	i.sys_health()
	
print "__________________________________________________________"
print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	
#Save mechanism
saveAs = raw_input("Save file as: ") 
saveAs = saveAs + ".xls"
print "Saving as: %s" % saveAs
wb.save(saveAs) #May want to automate this at some point for easy of use and standardization reasons.




Is This A Good Question/Topic? 0
  • +

Replies To: How to open a spreed sheet with python.

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,415
  • Joined: 12-December 12

Re: How to open a spreed sheet with python.

Posted 22 August 2014 - 12:53 PM

There are a few approaches at this SO topic of how to launch Excel and open a file.

In particular,
os.system("start Excel.exe <path/to/file>")

On Windows it is not necessary to specify the path to Excel.exe as file association will use the correct application.

Searching for this is better with the words "launch" or "start" rather than "open", as "open" will first find all the Excel-parsing modules.
Was This Post Helpful? 1
  • +
  • -

#3 jgrimmie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-August 14

Re: How to open a spreed sheet with python.

Posted 22 August 2014 - 12:54 PM

View Postandrewsw, on 22 August 2014 - 12:51 PM, said:

There are a few approaches at this SO topic of how to launch Excel and open a file.

In particular,
os.system("start Excel.exe <path/to/file>")

On Windows it is not necessary to specify the path to Excel.exe as file association will use the correct application.


Thanks this is not only what I needed but this will speed up my development as I wont have to find and open the file after each test run. Also its good to know how to do this in general so THANKS!
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,415
  • Joined: 12-December 12

Re: How to open a spreed sheet with python.

Posted 22 August 2014 - 12:58 PM

You're welcome.
Was This Post Helpful? 1
  • +
  • -

#5 jgrimmie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-August 14

Re: How to open a spreed sheet with python.

Posted 22 August 2014 - 01:00 PM

The new code:

##crac unit reporting...
import xlwt
from datetime import datetime
import os

class Unit():
	def __init__(self, name="unknown", temperature=0, humitty = 0):
		self.name = name
		self.temperature = temperature
		self.humitty = humitty
		
	def sys_health(self):
		if self.temperature > 85:
			#print self.temperature #for debug use
			print "Temperature is out of tolerance!"
		else:
			print "Temperature is within tolerance!"
		if self.humitty > 60:
			#print self.humitty #for debug use
			print "Humitty is out of tolerance!"
		else:
			print "Humitty is within tolerance!"
			
wb = xlwt.Workbook()
ws = wb.add_sheet("A test sheet") #this is to create the sheet but I need to change this to something relevant later

#This is the first Row/Column to be written to for the crac unit's ID.
unitIdRow = 1
unitIdColumn = 1

#This is the first Row/Column to be written to for the first units temperature.
unitTempRow = 3 
unitTempColumn = 1

#This is the first Row/Column to be written to for the first units humitty.
unitHumittyRow = 3
unitHumittyColumn = 2

#This is the first Row/Column to be written to for the Designation of temperature column's.
tempDesignationRow = 2
tempDesignationColumn = 1

#This is the first Row/Column to be written to for the Designation of humitty column's.
humittyDesignationRow = 2
humittyDesignationColumn = 2

active_units = ['crac-1', 'crac4', 'crac5', 'crac9', 'crac10', 'crac12', 'crac15', 'crac16', 'crac17', 'crac18', 'crac19', 'crac20', 'crac21', 'crac22', 'crac23', 'crac24', 'crac26']
#Need to have a way of altering this list 

for I in active_units: # This loop is for printing the headers that should not be reprinted.
	ws.write( tempDesignationRow, tempDesignationColumn, "Temp.")
	tempDesignationColumn = tempDesignationColumn + 2
	
	ws.write( humittyDesignationRow, humittyDesignationColumn, "Humitty")
	humittyDesignationColumn = humittyDesignationColumn + 2
	
	

for i in active_units: #This loop is for getting temp and humitty from the user and writing them to spreadsheet.
	print "__________________________________________________________"
	
	print "Current unit %s " % i
	ws.write( unitIdRow, unitIdColumn, i)
	unitIdColumn = unitIdColumn + 2
	
	currentUnitTemp = int(raw_input("Enter the current temperature for unit %s: " % i))
	ws.write( unitTempRow, unitTempColumn, currentUnitTemp)
	unitTempColumn = unitTempColumn + 2

	
	currentUnitHumitty = int(raw_input("Enter the current humitty for unit %s: " % i))
	ws.write( unitHumittyRow, unitHumittyColumn, currentUnitHumitty)
	unitHumittyColumn = unitHumittyColumn + 2

	
	i = Unit(i, currentUnitTemp, currentUnitHumitty)
	i.sys_health()
	
print "__________________________________________________________"
print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	
#Save mechanism
saveAs = raw_input("Save file as: ") 
saveAs = saveAs + ".xls"
print "Saving as: %s" % saveAs
wb.save(saveAs) #May want to automate this at some point for easy of use and standardization reasons.

os.system("start %s" % saveAs)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1