首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Cloudmersive响应写入str (字节)到PDF文件,避免文件损坏

如何从Cloudmersive响应写入str (字节)到PDF文件,避免文件损坏
EN

Stack Overflow用户
提问于 2022-11-30 19:36:25
回答 1查看 31关注 0票数 0

我目前正在使用Cloudmersive (.csv )将几种不同的文件格式( .xlsx、.docx、.one)转换为.pdf输出。他们的文档没有详细说明转换过程中来自API_response的编码类型。我尝试过几种不同的方法来编写api_response (输出: str (字节))。它似乎成功地写入了一个.pdf文件,但当我打开它时,Adobe表示该文件已损坏。

我试过检测编码的类型,但是chardet没有找到编码。

代码语言:javascript
复制
    configuration = cloudmersive_convert_api_client.Configuration()
    configuration.api_key['Apikey'] = 'PUT YOUR KEY HERE' #individual user-id linked to the account
    
    # create an instance of the API class
    api_instance = cloudmersive_convert_api_client.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))

    # Convert Document to PDF
    if (os.stat(input_file).st_size != 0): #api does not work on empty files
        
        try:
            api_response = api_instance.convert_document_ppt_to_pdf(input_file) #ONLY DIFFERENCE
            
            os.remove(input_file) 
            output_file=os.path.splitext(input_file)[0]+".pdf"
            
            with open(output_file, 'wb') as binary_file:
                binary_file.write(bytearray(str(api_response),encoding='utf-8'))
                
            print(input_file, 'was processed by ConvertDocumentPptToPdf.')
            
        except ApiException as e:
            print(input_file, 'was not processed.')

我也尝试过,但这也不起作用:

代码语言:javascript
复制
            with open(output_file, 'wb') as binary_file:
                binary_file.write(bytearray(api_response))

下面是API响应(Api_response)的一些示例输出:

代码语言:javascript
复制
b'b\'%PDF-1.5\\n%\\xc3\\xa4\\xc3\\xbc\\xc3\\xb6\\xc3\\x9f\\n2 0 obj\\n<</Length 3 0 R/Filter/FlateDecode>>\\nstream\\nx\\x9c\\x85TM\\x8b\\xdc0\\x0c\\xbd\\xe7W\\xf8\\xbc\\x10\\xaf$

另外,当我尝试检测编码时,它会说:检测= chardet.detect(test.encode()) print(检测)

代码语言:javascript
复制
{'encoding': None, 'confidence': 0.0, 'language': None}
EN

回答 1

Stack Overflow用户

发布于 2022-12-01 16:51:17

下列代码按照评论中的建议起作用:

代码语言:javascript
复制
import ast

# create an instance of the API class
api_instance = cloudmersive_convert_api_client.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))

# Convert Document to PDF
if (os.stat(input_file).st_size != 0): #api does not work on empty files
    
    try:
        api_response = api_instance.convert_document_ppt_to_pdf(input_file) #ONLY DIFFERENCE
        
        os.remove(input_file) 
        output_file=os.path.splitext(input_file)[0]+".pdf"
        
        data = ast.literal_eval(api_response)
        
        with open(output_file, 'wb') as binary_file:
            binary_file.write(data)
            
        print(input_file, 'was processed by ConvertDocumentPptToPdf.')
        
    except ApiException as e:
        print(input_file, 'was not processed.')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74633253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档