我目前正在使用Cloudmersive (.csv )将几种不同的文件格式( .xlsx、.docx、.one)转换为.pdf输出。他们的文档没有详细说明转换过程中来自API_response的编码类型。我尝试过几种不同的方法来编写api_response (输出: str (字节))。它似乎成功地写入了一个.pdf文件,但当我打开它时,Adobe表示该文件已损坏。
我试过检测编码的类型,但是chardet没有找到编码。
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.')我也尝试过,但这也不起作用:
with open(output_file, 'wb') as binary_file:
binary_file.write(bytearray(api_response))下面是API响应(Api_response)的一些示例输出:
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(检测)
{'encoding': None, 'confidence': 0.0, 'language': None}发布于 2022-12-01 16:51:17
下列代码按照评论中的建议起作用:
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.')https://stackoverflow.com/questions/74633253
复制相似问题