标签: pyexcel

Python - 如何更新 ODS 文件而不丢失格式

在我的项目中,我使用 Python 和 pyexcel-ods3 库,并且需要更改 ods 文件中某些单元格的值。

我设法做到了,但我在单元格中放入的每个参数(例如颜色、合并等)都已重置。

这是代码

from pyexcel_ods3 import get_data
from pyexcel_ods3 import save_data
from collections import OrderedDict

def get_data_file(file):
    return get_data(file)

def write_cell_sheet(filename, sheet, line, column, donnees_file, new_value):
    donnees_file[sheet][int(line) - 1][int(column) - 1] = new_value
    save_data(filename, donnees_file)

donnees_file = get_data_file(odsfile)
write_cell_sheet(odsfile, "Sheet1", 5, 5, donnees_file, "HELLO")
Run Code Online (Sandbox Code Playgroud)

此代码并非来自我的项目,但它显示了如何更新我的 ods 文件。

这是相应文件的两张图片,以更好地描述我的问题

更新前:

在此输入图像描述

更新后:

在此输入图像描述

如果有人知道如何更新我的 ods 文件而不丢失格式,我将非常高兴知道。

python pyexcel

5
推荐指数
0
解决办法
621
查看次数

pyexcel导出错误"没有内容,文件名.没有给出任何东西"

我正在使用django-pyexcel从网站导出数据,但当我转到导出URL时,我收到错误:

异常类型:IOError

例外值:没有内容,文件名.没有给出任何东西

导出数据的代码是从文档中给出的示例中复制的:

return excel.make_response_from_a_table(Question, 'xls', file_name="sheet")
Run Code Online (Sandbox Code Playgroud)

python django pyexcel django-excel

3
推荐指数
1
解决办法
1617
查看次数

使用flask返回创建的excel文件

我正在使用 openpyxl 创建一个 excel 文件,我想将其作为文件下载返回(因此不保存在本地)。

我可以很好地创建 Excel 文件并将其保存到磁盘。但是,我无法下载该文件。

尝试1:

import flask_excel as excel

...

create_excel_sheet(data) # internally save the sheet with name sheet.xlsx

output = excel.make_response()
output.headers["Content-Disposition"] = "attachment; filename=" + \
                                        'sheet.xlsx'
output.headers["Content-type"] = "application/vnd.openxmlformats-\
officedocument.spreadsheetml.sheet"

return output
Run Code Online (Sandbox Code Playgroud)

这将返回一个名为sheet.xlsx的空文本文件

尝试 2: wb = create_excel_sheet(data) # 返回 openpyxl 工作簿

output = excel.make_response(wb)
output.headers["Content-Disposition"] = "attachment; filename=" + \
                                        'sheet.xlsx'
output.headers["Content-type"] = "application/vnd.openxmlformats-\
officedocument.spreadsheetml.sheet"

return output
Run Code Online (Sandbox Code Playgroud)

我不想使用 pyexcel 来获取数据,因为我需要 openpyxl 来创建一个精美的 Excel 工作表。显然,如果 pyexcel 和 openpyxl 能够通信那就没问题了。

有什么想法吗?

干杯,迈克

python excel flask openpyxl pyexcel

3
推荐指数
1
解决办法
1万
查看次数

pyexcel get_book 和 get_records 函数抛出 XLSX 文件异常

我正在尝试使用打开 XLSX 文件pyexcel。但两者都失败get_bookget_records出现以下错误。但是,如果我尝试读取转换为xls它的相同文件,它确实有效。我获取用户上传的文件:所以不能限制上传XLSX格式的文件。

>>> import pyexcel

>>> workbook = pyexcel.get_book(file_name='Sample_Employee_data_xls.xlsx')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/me/env/lib/python3.10/site-packages/pyexcel/core.py", line 47, in get_book
    book_stream = sources.get_book_stream(**keywords)
  File "/home/me/env/lib/python3.10/site-packages/pyexcel/internal/core.py", line 38, in get_book_stream
    sheets = a_source.get_data()
  File "/home/me/env/lib/python3.10/site-packages/pyexcel/plugins/sources/file_input.py", line 38, in get_data
    sheets = self.__parser.parse_file(self.__file_name, **self._keywords)
  File "/home/me/env/lib/python3.10/site-packages/pyexcel/plugins/parsers/excel.py", line 19, in parse_file
    return self._parse_any(file_name, **keywords)
  File "/home/me/env/lib/python3.10/site-packages/pyexcel/plugins/parsers/excel.py", line 40, in _parse_any
    sheets = get_data(anything, file_type=file_type, **keywords)
  File "/home/me/env/lib/python3.10/site-packages/pyexcel_io/io.py", line …
Run Code Online (Sandbox Code Playgroud)

python excel xlsx pyexcel

3
推荐指数
1
解决办法
941
查看次数

将 Dataframe 写入 ODS 格式

我正在使用pandas库读取.xlxs文件并提取.xlxs文件。现在我正在尝试创建一个扩展名为.ods的文件,并使用pyexcel_ods库将其写入其中。这是我的代码:--dataframedataframe

import pandas as pd
from pyexcel_ods import save_data
from collections import OrderedDict

self.current_df = pd.read_excel('filename')
data = OrderedDict()
data.update(df)
save_data("as.ods", data)
Run Code Online (Sandbox Code Playgroud)

它正在抛出错误

{TypeError}'int' 对象不可迭代

随意索取更多代码。

注意:--我使用的是 Python 3。

python python-3.x pandas pyexcel

2
推荐指数
2
解决办法
3903
查看次数

标签 统计

pyexcel ×5

python ×5

excel ×2

django ×1

django-excel ×1

flask ×1

openpyxl ×1

pandas ×1

python-3.x ×1

xlsx ×1