我正在使用openpyxl
并拥有一个具有动态变化结构的文件。我应该做一个解析器,它基于单元格的值与列表 paren 的元素的匹配category = [pc1, pc2...]
,给出单元格的坐标。但问题是我不能使用按父类别名称搜索,因为这些名称不是唯一的,而且经常在文本中。接下来的步骤我决定按样式关联搜索,而不是按文本匹配。在文件中,有关父类别的信息包含在某种颜色的合并单元格中。我编写了一个解析器,它通过颜色及其属性找到我需要的单元格 - 合并单元格。这种方式适用于 xlsx 格式。但我还需要使用旧的 xls 格式。要从 转换xls
为xlsx
,我使用pyexcel
库。
if filename.endswith('.xls'):
import pyexcel
_f, _ = filename.split('.')
pyexcel.save_book_as(file_name=file, dest_file_name=f'{_f}.xlsx')
但事实证明,在转换时,样式属性的传输以某种方式被破坏,并且不可能通过颜色或合并属性接收单元格的坐标。
使用合并单元格
workbook = load_workbook(filename=file)
sheet = workbook["RFI"]
# get the list of first cell of merged cell coordinate
list_of_first_coordinate_in_merget_cell = [cell.__str__().split(':')[0] for cell in sheet.merged_cell_ranges]
for range_ in sheet.merged_cell_ranges:
# get current coordinate from all merget cell and set it as a string
cell_obj_to_str = (range_.__str__())
在这种情况下merged_cell_ranges
不起作用
使用颜色
def test_excel_file_response(file):
pc_coordinate = {}
workbook = load_workbook(filename=file)
sheet = workbook["RFI"]
for row_cells in sheet.iter_rows(min_row=4):
for cell in row_cells:
if cell.value in paren_category_ and cell.fill.start_color.rgb:
pc_coordinate[cell.value] = cell.coordinate
print(cell.value, cell.fill.start_color)
return pc_coordinate
在这种情况下cell.fill.start_color
不起作用
我在 github 上就这些主题创建了问题。 https://github.com/pyexcel/pyexcel/issues/206 https://github.com/pyexcel/pyexcel/issues/207
所有转换信息都基于使用 Windows 的pyexcel
orwin32com
库(我使用的是 Ubuntu)。是否还有其他适用于 Ubuntu 和 Python3.7 并在处理过程中保存样式的转换方法?欢迎任何建议或建议,因为我处于完全停滞状态......