SoFunction
Updated on 2024-11-16

Python-based way to use Yonkou Document Conversion Service

I recently started learning Python because of my work; and the project uses theYongzhong Document Conversion Service, and thought about practicing with this. Relying on the ease of use of Python, it was done in no time. Now summarize it as a simple learning note for both.

1 Uploading files and converting them

After checking the corresponding interface documentation, let's try uploading a file first:

def upload():
    url = 'http://172.18.21.87:48080/fcscloud/file/upload'
    header = {"Content-Type": "multipart/form-data"}
    file = {"file": open('', 'rb')}
    result = (url=url, files=file).json()
    print((result, sort_keys=True, indent=2, ensure_ascii=False))

Successful operation, the file has been uploaded, data is the relative path of the file; know the return data, you can then call convert interface for conversion:

def upload():
    url = 'http://172.18.21.87:48080/fcscloud/file/upload'
    header = {"Content-Type": "multipart/form-data"}
    file = {"file": open('', 'rb')}
    result = (url=url, files=file).json()
    return result['data']['data']
def convert(srcRelativePath):
    url = 'http://172.18.21.87:48080/fcscloud/composite/convert'
    data = {'convertType': 61, 'srcRelativePath': srcRelativePath}
    result = (url=url, data=data).json()
    print((result, sort_keys=True, indent=2, ensure_ascii=False))
if __name__ == '__main__':
    convert(upload())    

This change, the upload interface to return the results, as a parameter to convert, and specify convertType is the type of conversion (for office files can be used 61), the same return a json result.

json results contain some basic information, such as conversion time, file information, conversion type, and error codes, etc.; and viewUrl is the address of the document online preview, copy the browser to open to see the following

2 Composite Interface

Just now is the first upload files, and then converted, there is no simpler way to combine the two into one? Yongzhong document conversion service provides such a composite interface:

def upload_and_convert():
    url = 'http://172.18.21.87:48080/fcscloud/composite/upload'
    header = {"Content-Type": "multipart/form-data"}
    file = {"file": open('', 'rb')}
    data = {"convertType": 61,                   # Conversion types
            "wmColor":"blue",                    #Watermark color
            "wmContent":"Yonaka Document Conversion Service",       #Watermarked content
            "wmSize":"50",                       #Watermark size
            "wmRotate":0}                        #Watermark angle
    result = (url=url, files=file, data=data).json()
    # print(result)
    print(result['data']['viewUrl'])

According to the above show in the json results, you can enrich the scene and business, such as according to the error code to determine whether the conversion is successful and so on, this side in order to show the effect, temporarily did not take into account some of the exceptions, and only take the viewUrl preview; at the same time in the parameter, the additional addition of a number of watermarking related parameters, see how the effect of watermarking:

3 Conversion interfaces for online documents

Considering that many documents are online nowadays, instead of uploading them manually, Yonkers document conversion service also provides a conversion interface for online documents:

def http_file():
    url = 'http://172.18.21.87:48080/fcscloud/composite/httpfile'
    data = {"convertType": 61,
            'fileUrl': "http://172.18.21.87:48080/fcscloud/view/preview/gN-L5i-rqfV3L-5YwdyhbocM4AFZ1qLaNOfR58wqmPzq5yl6Ozaox5FtkOeT3U2dcGH7iZIGEkHe2cYS19931fYVW1WmGxqScQcqTmqfF4k0XJ0n9T43tS0XAWUC9i7ylCA8riwx8vqdy7cdU_MPpdgABhrxJFXkl6fhGj__qRG6-3WWebp0p1RxMpuuGaZNpYzDMjwIfyBsOskCvCMmx1Zm9_hzBCke2CNkWIw2a6lrQXYRDSmEwT8IHxRFsYnKfQ0RnNAc5xTKZVi4ovzMANI5IY9vfOIBLjZyTH4XSj9aAzvjNn69flAzLSbU4aGzgU3VyTFbTwIO3jgfWCPmDYBxOikkjBh7bji6xtunWsA=/"}
    result = (url=url, data=data).json()
    print(result['data']['viewUrl'])

Again take only the viewUrl preview and visit the link in the browser

If the following error occurs, it means that the online file could not be downloaded, which makes sense when you think about it: if you can't even get the file, how can you convert and preview it?

4 Summary

Well, aboutYongzhong Document ConversionThis is the first introduction to the service. Overall, the interface call is simple, plus the use of the process is not complicated, and the file preview is also very good. Of course, since I just came into contact with Python, there are shortcomings, please advise you more, ha.

To this article on the use of Python based document conversion services in the way the Wing in the article is introduced to this, more related to Python Wing in the document conversion services content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!