(file)
| 74 | |
| 75 | |
| 76 | def main(file): |
| 77 | # 上传文件并等待解析完成 |
| 78 | upload_data = preupload() |
| 79 | print(upload_data) |
| 80 | url, uid = upload_data["url"], upload_data["uid"] |
| 81 | |
| 82 | put_file(file, url) |
| 83 | |
| 84 | for _ in range(100): |
| 85 | status_data = get_status(uid) |
| 86 | status = status_data.get("status") |
| 87 | if status == "success": |
| 88 | print("Save result to result.json") |
| 89 | with open("result.json", "w") as f: |
| 90 | json.dump(status_data["result"], f) |
| 91 | break |
| 92 | elif status == "failed": |
| 93 | print(status_data) |
| 94 | raise Exception(f"parse failed: {status_data.get('detail')}") |
| 95 | elif status == "processing": |
| 96 | print(status_data) |
| 97 | print(f"progress: {status_data.get('progress')}") |
| 98 | time.sleep(3) |
| 99 | else: |
| 100 | raise Exception(f"Fails to deal with uid: {uid} after 100 retries") |
| 101 | |
| 102 | # 导出文件 |
| 103 | print("Start exporting file...") |
| 104 | export_file(uid, "docx", "normal") # 可以根据需要修改格式和公式模式 |
| 105 | |
| 106 | for _ in range(100): |
| 107 | result_data = get_export_result(uid) |
| 108 | status = result_data.get("status") |
| 109 | if status == "success": |
| 110 | file_url = result_data["url"] |
| 111 | output_path = "output.docx" # 根据实际格式修改扩展名 |
| 112 | print(f"Downloading file to {output_path}") |
| 113 | download_file(file_url, output_path) |
| 114 | return |
| 115 | elif status == "failed": |
| 116 | print(result_data) |
| 117 | raise Exception("Export failed") |
| 118 | elif status == "processing": |
| 119 | print("Export processing...") |
| 120 | time.sleep(3) |
| 121 | raise Exception(f"Export timeout with uid: {uid} after 100 retries") |
| 122 | |
| 123 | |
| 124 | if __name__ == "__main__": |
no test coverage detected