Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.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/6/apache/9.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 更改单元格后,openpyxl会破坏文件格式_Python_Openpyxl - Fatal编程技术网

Python 更改单元格后,openpyxl会破坏文件格式

Python 更改单元格后,openpyxl会破坏文件格式,python,openpyxl,Python,Openpyxl,我的.xlsx格式(UPO模板)有一些奇怪的格式 模板如下所示: 更改任何单元格中的值后,格式将被破坏,图像将消失: 更改单元格的代码如下所示: wb2 = load_workbook('template.xlsx') ws2 = wb2.active ws2['A1'] = user.name wb2.save('template.xlsx') 标准的东西,但有些不对劲。我应该使用另一个模块而不是openpyxl吗?这是一个已知的错误,在openpyxl的BitBucket问题列表中

我的.xlsx格式(UPO模板)有一些奇怪的格式

模板如下所示:

更改任何单元格中的值后,格式将被破坏,图像将消失:

更改单元格的代码如下所示:

wb2 = load_workbook('template.xlsx')
ws2 = wb2.active

ws2['A1'] = user.name

wb2.save('template.xlsx')

标准的东西,但有些不对劲。我应该使用另一个模块而不是openpyxl吗?

这是一个已知的错误,在openpyxl的BitBucket问题列表中标记为已解决。但是,它仍然不起作用,您必须使用kseehart/db2053建议的补丁

我在这里为您复制了补丁:
(免责声明:这不是我创建的,它是由kseehart/db2053作为解决方案提出的)


图像不支持读取。除了图像,当你说格式被破坏时,你的意思是单元格大小被更改了?
def patch_worksheet():
"""This monkeypatches Worksheet.merge_cells to remove cell deletion bug
https://bitbucket.org/openpyxl/openpyxl/issues/365/styling-merged-cells-isnt-working
Thank you to Sergey Pikhovkin for the fix
"""

    def merge_cells(self, range_string=None, start_row=None, start_column=None, end_row=None, end_column=None):
        """ Set merge on a cell range.  Range is a cell range (e.g. A1:E1)
        This is monkeypatched to remove cell deletion bug
        https://bitbucket.org/openpyxl/openpyxl/issues/365/styling-merged-cells-isnt-working
        """
        if not range_string and not all((start_row, start_column, end_row, end_column)):
            msg = "You have to provide a value either for 'coordinate' or for\
            'start_row', 'start_column', 'end_row' *and* 'end_column'"
            raise ValueError(msg)
        elif not range_string:
            range_string = '%s%s:%s%s' % (get_column_letter(start_column),
                                      start_row,
                                      get_column_letter(end_column),
                                      end_row)
        elif ":" not in range_string:
            if COORD_RE.match(range_string):
                return  # Single cell, do nothing
            raise ValueError("Range must be a cell range (e.g. A1:E1)")
        else:
            range_string = range_string.replace('$', '')

        if range_string not in self.merged_cells:
            self.merged_cells.add(range_string)


        # The following is removed by this monkeypatch:

        # min_col, min_row, max_col, max_row = range_boundaries(range_string)
        # rows = range(min_row, max_row+1)
        # cols = range(min_col, max_col+1)
        # cells = product(rows, cols)

        # all but the top-left cell are removed
        #for c in islice(cells, 1, None):
            #if c in self._cells:
                #del self._cells[c]

    # Apply monkey patch
    worksheet.Worksheet.merge_cells = merge_cells

patch_worksheet()