Skip to content

Commit

Permalink
Start testing cloud convert API
Browse files Browse the repository at this point in the history
Handled several issues here:
1. The environment variables are wrongly hardcoded, and you can't
  use the alternative of providing your own API Keys from elsewhere.
  See cloudconvert/cloudconvert-python#13 and
  cloudconvert/cloudconvert-python#24. Will
  need to fix this to be able to run in docassemble.
2. Imports/uploading files: you need to follow this example: https://github.com/cloudconvert/cloudconvert-python#uploading-files
3. The output document somehow has more validation errors, but still opens fine in office.com.
  For some reason, the office converter doesn't convert from RTF to DOCX,
  so it uses the libreoffice converter instead, which generates all of those
  validation errors. Will just have to go with it, as long as some more obscure choices work
  in all of the testing files (drive, office 365, desktop office, and libreoffice).

  Details: combining from GDocs: 9 errors:
  * ExceptionError: Inner exception: 'a' is an undeclared prefix.
  * several Sch_AttributeValueDataTypeDetaileds, with in:w and in:hanging with invalid values (0.0 or 115.0 not valid Int16)

  from combining from cloudconvertAPI: 635 errors
  * mostly Sch_UnexpectedElementContentExpectingComplex: has unexpecetd child element 'main:b'
  * Sch_MissRequiredAttribute: the required attribute 'val' is missing
  * Sch_AttributeValueDataTypeDetailed: the attribute main:val has invalid value 'end' (sometimes 'start')

Resources to use when picking back up:
* https://sandbox.cloudconvert.com/dashboard/jobs#
* https://cloudconvert.com/api/v2/jobs/builder#
* https://cloudconvert.com/api/v2/import#import-url-tasks
  • Loading branch information
BryceStevenWilley committed Jun 4, 2022
1 parent a161dfe commit abecf65
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions test_cloudconvert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import cloudconvert
from docxtpl import DocxTemplate, RichText
import os

def merge(all_docs, out_name):
tpl = DocxTemplate('docassemble/tclpgoogledocsmerger/data/templates/ClausesTemplate_fixed.docx')
subdocs = []
for doc in all_docs:
link = RichText('Online resources at no where')
subdocs.append((doc, 'mydate', link, tpl.new_subdoc(doc)))

context = {
'mysubdoc': subdocs,
'today_date': 'mydate_global',
'tags_with_rows': [],
}

tpl.render(context)
tpl.save(out_name)
return

def use_cloudconvert(in_name):
cloudconvert.default()

job = cloudconvert.Job.create(payload={"tasks": {
'import-my-file': {
'operation': 'import/upload',
},
'convert-my-file': {
'operation': 'convert',
'input': 'import-my-file',
'input_format': 'docx',
'output_format': 'rtf',
'engine': 'office',
},
'convert-my-file-back': {
'operation': 'convert',
'input': 'convert-my-file',
'input_format': 'rtf',
'output_format': 'docx',
# 'engine': 'office',
},
'export-my-file': {
'operation': 'export/url',
'input': 'convert-my-file-back'
}
},
"tag": "jobbuilder",
})

upload_task_id = job['tasks'][0]['id']
upload_task = cloudconvert.Task.find(id=upload_task_id)
res = cloudconvert.Task.upload(file_name=in_name, task=upload_task)
print(f'Task.upload res: {res}')
res = cloudconvert.Task.find(id=upload_task_id)
print(f'Task.find res: {res}')
job = cloudconvert.Job.wait(id=job['id'])

if 'tasks' not in job:
print(f'failed!: {job}')
return


for task in job["tasks"]:
if task.get("name") == "export-my-file" and task.get("status") == "finished":
export_task = task
break

file_info = export_task.get("result").get("files")[0]
cloudconvert.download(filename='post-' + file_info['filename'], url=file_info['url'])


if __name__ == '__main__':
merge(['aatmay.docx', 'robyn.docx', 'elsie.docx', 'aiden.docx'], 'pre-convert.docx')
use_cloudconvert('aatmay.docx')
use_cloudconvert('robyn.docx')
use_cloudconvert('elsie.docx')
use_cloudconvert('aiden.docx')
merge(['post-aatmay.docx', 'post-robyn.docx', 'post-elsie.docx', 'post-aiden.docx'], 'post-convert.docx')


0 comments on commit abecf65

Please sign in to comment.