Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否将Excel zip文件内容转换为实际的Excel文件?_Python_Cmis - Fatal编程技术网

Python 是否将Excel zip文件内容转换为实际的Excel文件?

Python 是否将Excel zip文件内容转换为实际的Excel文件?,python,cmis,Python,Cmis,我正在使用python中提供的cmis包从FileNet存储库下载文档。我正在使用包中提供的getcontentstream方法。但是,它返回的内容文件以“Pk”结尾,以“Pk”结尾。当我在谷歌上搜索时,我知道这是excel的zip包内容。是否有办法将内容保存到excel文件中。我应该能够打开下载的excel。我正在使用下面的代码。但获取类似字节的对象是必需的,而不是str。我注意到结果的类型是string.io 大家好,欢迎来到stackoverflow。关于你的帖子,我注意到了一些地方 回答

我正在使用python中提供的cmis包从FileNet存储库下载文档。我正在使用包中提供的getcontentstream方法。但是,它返回的内容文件以“Pk”结尾,以“Pk”结尾。当我在谷歌上搜索时,我知道这是excel的zip包内容。是否有办法将内容保存到excel文件中。我应该能够打开下载的excel。我正在使用下面的代码。但获取类似字节的对象是必需的,而不是str。我注意到结果的类型是string.io


大家好,欢迎来到stackoverflow。关于你的帖子,我注意到了一些地方

回答您直接得到的错误代码。您调用的outfile文件流是二进制的,但是result.read必须是Unicode字符串格式,这就是为什么会出现此错误。在将其传递给outfile.write函数ex:outfile.writeresult.read.encode之前,可以尝试对其进行编码

您也可以通过以下方式直接编写Unicode:

result = testDoc.getContentStream()
result_text = result.read()

from zipfile import ZipFile

with ZipFile(filepath, 'w') as zf:
    zf.writestr('filename_that_is_zipped', result_text)
我不确定您的ContentStream中有什么内容,但请注意excel文件是由压缩的xml文件组成的。excel文件所需的最低文件结构如下:

_rels/.rels包含excel架构 docProps/app.xml包含图纸数量和图纸名称 docProps/core.xml锅炉板用户信息和创建日期 xl/workbook.xml包含工作表名称rdId到工作簿链接 此文件夹中的xl/worksheets/sheet1.xml和更多工作表包含每个工作表的单元格数据 xl/_rels/workbook.xml.rels包含zipfile中的图纸文件位置 xl/sharedStrings.xml,如果只有字符串单元格值 [Content_Types].xml将架构应用于文件类型
我最近从头开始拼凑了一个excel文件,如果你想看到代码签出

Maybe.encodelatin-1获得字节对象的输出?使用zipfile包解压文件/stream->Python的标准库中没有读写excel.xlsx格式文件的模块,因此,您可能需要从第三方找到并安装一个,或者编写您自己的代码来完成。@Wouterr。谢谢你的回复。成功了。我用latin-1编码,它将字符串转换成字节,我成功地打开了excel文件,没有任何问题。我的代码工作代码如下。result=testDoc.getContentStream outfile=opensample.xlsx,“wb”outfile.writeresult.read.encode'latin-1'result.close outfile。close@Bitonator. 谢谢你看我的问题,谢谢你的回答。感谢您的回复。成功了。我用latin-1编码,它将字符串转换成字节,我成功地打开了excel文件,没有任何问题。我的代码工作代码如下。result=testDoc.getContentStream outfile=opensample.xlsx,“wb”outfile.writeresult.read.encode'latin-1'result.close outfile.close
result = testDoc.getContentStream()
result_text = result.read()

from zipfile import ZipFile

with ZipFile(filepath, 'w') as zf:
    zf.writestr('filename_that_is_zipped', result_text)