Edited to add: mere days after I posted this, Quip added built-in support for spreadsheets. So…this is no longer useful. Oh well.

Looks over your recently-edited Quip documents. For each doc with "Tabular" in the name, find the first table in the document, save that table's data as a .TSV file (import format for Spreadsheets). Nice for the case in which several folks want to collaborate on gathering data "in the field" on their phones, but you eventually want to crunch that data in a spreadsheet.

# Copyright 2014 Larry Hosken
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Retrieves quip pages, saves tables as .tsv"""

# Along with this file, you'll also need and :

import token # one-line that says TOKEN='1234adsf' , but using token
             # from

import quip  # copy

import csv
import xml.etree.cElementTree
import xml.sax.saxutils

def main():
    client = quip.QuipClient(access_token=token.TOKEN)

def fetch_tables(client):
    threads = client.get_recent_threads(count=20)
    for key, thread in threads.items():
      munge_thread(thread, client)

def munge_thread(thread, client):
    title = thread["thread"]["title"]
    sanitized_title = ''.join([c for c in title if c.isalnum()])
    if not "html" in thread: return
    if not "tabular" in sanitized_title.lower(): return

    # Parse the document
    tree = client.parse_document_html(thread["html"])
    for el in tree.iter('table'):
      table_el = el
      print 'doc %s named "tabular" but has no table?'
    csvfile = open(sanitized_title + '.tsv', 'wb')
    csvwriter = csv.writer(csvfile, delimiter='\t')
    for tr_el in table_el.iter('tr'):
      row = []
      for td_el in tr_el.iter('th'):
        t = ''
        for i in td_el.itertext(): t += i
      if row: csvwriter.writerow(row)
    for tr_el in table_el.iter('tr'):
      row = []
      for td_el in tr_el.iter('td'):
        t = ''
        for i in td_el.itertext(): t += i
      if row: csvwriter.writerow(row)

    html = unicode(xml.etree.cElementTree.tostring(tree))
    # Strip the <html> tags that were introduced in parse_document_html
    html = html[6:-7]

    document_file_name = sanitized_title + ".html"
    with open(document_file_name, "w") as document_file:

if __name__ == '__main__':


comment? | | home |