Created
March 23, 2017 11:41
-
-
Save SergioCC14/cf50dd90ac07352691b3bc8dd543b121 to your computer and use it in GitHub Desktop.
Example: OpenOffice SDK (spreadsheet manage) with Python & Flask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from flask import Flask | |
from flask import request | |
application = Flask(__name__) | |
import pyoo | |
import os | |
try: | |
spreadsheet_dir_path = sys.argv[1] | |
spreadsheet_path = spreadsheet_dir_path + str(os.listdir(spreadsheet_dir_path)[-1]) | |
desktop = pyoo.Desktop('localhost', 2002) | |
doc = desktop.open_spreadsheet(spreadsheet_path) | |
except OSError: | |
print("No such file or directory: ["+ sys.argv[1] +"]") | |
quit() | |
except: | |
print("Where is OpenOffice connection? This is not in 0.0.0.0:2002...") | |
quit() | |
def shutdown_server(): | |
func = request.environ.get('werkzeug.server.shutdown') | |
if func is None: | |
raise RuntimeError('Not running with the Werkzeug Server') | |
func() | |
# ROUTES | |
@application.route("/calculate_secret_data", methods=['GET']) | |
def calculate_secret_data(): | |
params = { | |
'current_age': request.args.get('current_age'), | |
'current_date': request.args.get('current_date'), | |
} | |
sheet = doc.sheets[0] | |
sheet[1,2].value = int(params['current_age']) | |
sheet[2,2].value = int(params['current_data']) | |
results = sheet_out[1,3].value | |
return str(results) | |
@application.route('/shutdown', methods=['POST']) | |
def shutdown(): | |
try: | |
doc.close() | |
except: | |
print("Doc cannot be close (where is Open Office?)") | |
print("Forcing shutdown: Secret Data Calculator API cannot find Open Office doc to Close") | |
shutdown_server() | |
return 'Server shutting down...' | |
if __name__ == "__main__": | |
application.debug = True | |
application.run(host='0.0.0.0', port=5000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment