Skip to content

Instantly share code, notes, and snippets.

@nonsleepr
Created September 6, 2023 16:10
Show Gist options
  • Save nonsleepr/00cf771a7fbac4ef5f66d22a8839f488 to your computer and use it in GitHub Desktop.
Save nonsleepr/00cf771a7fbac4ef5f66d22a8839f488 to your computer and use it in GitHub Desktop.
OfficePy REPL
import requests
from io import BytesIO
import sys
import json
OFFICEPY_HOST = "service-preview-p1-eastus2.officepy.microsoftusercontent.com"
USER_AGENT = "Microsoft Office/16.0 (Windows NT 10.0; Microsoft Excel 16.0.16827; Pro)"
GREEN = '\033[32m'
RED = '\033[91m'
RESET = '\033[0m'
with open("officedata.json", "rt") as f:
"""
Expect officedata.json in the same directory with, e.g.:
{
"token": "eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkExMjhDQkMtSFMyNTYi.....",
"environment": "11111111-2222-3333-4444-555555555555",
"runtime": "66666666-7777-8888-9999-000000000000"
}
"""
data = json.load(f)
TOKEN = data["token"]
ENVIRONMENT = data["environment"]
RUNTIME = data["runtime"]
extra_kwargs = {
"proxies": {
"http": "http://127.0.0.1:8080",
"https": "http://127.0.0.1:8080"
},
"verify": False
}
def send_code(code: str):
url = f"https://{OFFICEPY_HOST}/proxy/api/environments/{ENVIRONMENT}/runtimes/{RUNTIME}/executelongoperation2?flags=2&timeout=30"
headers = {
"User-Agent": USER_AGENT,
"Authorization": f"Bearer {TOKEN}"
}
# Remove extra_kwargs reference if don't need the proxy
resp = requests.post(url, headers=headers, **extra_kwargs, files={"code": ("code.txt", BytesIO(code.encode()))})
try:
res = resp.json()
stdout = res["stdout"] + ((res["result"] or {}).get("value_text", "") or "")
stderr = res["stderr"] + (res["error_stack_trace"] or "")
except:
stdout = ""
stderr = resp.text
print(f"{GREEN}{stdout}{RESET}")
print(f"{RED}{stderr}{RESET}", file=sys.stderr)
if __name__ == "__main__":
while True:
code = input("$ ")
if code == "exit":
break
send_code(code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment