Here is my entire script:
from flask import Flask, request, jsonify, render_template_string
from flask_cors import CORS
import os
import openai
from dotenv import load_dotenv
import asposewordscloud
from asposewordscloud import WordsApi
from asposewordscloud.configuration import Configuration
from asposewordscloud.models.requests import UploadFileRequest, ConvertDocumentRequest
import io
import tempfile
load_dotenv()
app = Flask(__name__)
CORS(app)
aspose_client_id = os.getenv('ASPOSE_CLIENT_ID')
aspose_client_secret = os.getenv('ASPOSE_CLIENT_SECRET')
words_api = WordsApi(client_id=aspose_client_id, client_secret=aspose_client_secret)
# Load the document
request_document = open('test1.docx', 'rb')
request_paragraph = asposewordscloud.ParagraphInsert(text='Morbi enim nunc faucibus a.')
# Insert the paragraph
insert_paragraph_request = asposewordscloud.models.requests.InsertParagraphOnlineRequest(
document=request_document, paragraph=request_paragraph)
insert_paragraph_response = words_api.insert_paragraph_online(insert_paragraph_request)
# Print the response to understand its structure
print("Insert Paragraph Response:", insert_paragraph_response)
# Check if the response contains document bytes or other relevant data
document_bytes = insert_paragraph_response.document.get('document', None) if hasattr(insert_paragraph_response.document, 'get') else None
print("Document Bytes Response:", document_bytes)
if document_bytes:
# Save the updated document to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix='.docx') as temp_file:
temp_file.write(document_bytes)
temp_file_path = temp_file.name
# Convert the document
with open(temp_file_path, 'rb') as document_file:
convert_request = ConvertDocumentRequest(
document=document_file, format='docx')
print(f"Convert Request: {convert_request}")
convert_response = words_api.convert_document(convert_request)
print(f"Conversion Response: {convert_response}")
# Clean up the temporary file
os.remove(temp_file_path)
else:
print("No document bytes found in the response.")
This is the response it produces:
Insert Paragraph Response: <asposewordscloud.models.responses.insert_paragraph_online_response.InsertParagraphOnlineResponse object at 0x00000146A3B265A0>
Document Bytes Response: None
No document bytes found in the response.
Keep in mind that this is at best a marginal modification for the example given by Aspose. The test1.docx file is in the same folder.
Any help will be greatly appreciated.
Thank you.