使用数据和相应的单元格后附加行

时间:2017-09-14 15:40:23

标签: python excel openpyxl xlsxwriter

我试图使用OpenPyxl和xlsxwriter扩展excel中的行号一段时间。

现在我的文件看起来像:

enter image description here

我需要追加另一个与Shape相对应的数据,并将所有内容向下移动,所以它看起来像:

enter image description here

我将数据['a','b','c']存储为列表。我需要使用字典吗?或者任何简单的方法?

现在我的代码如下:

from openpyxl import load_workbook
from openpyxl import Workbook
import xlsxwriter
import collections

INPUT_FILE = 'output.xlsx'



wb = load_workbook(filename=INPUT_FILE)
ws = wb['cond']

workbook = xlsxwriter.Workbook('data.xlsx')
worksheet = workbook.add_worksheet()


def get_data(start, stop, col):
    mylist = [ws.cell(row=i,column= col).value for i in range(start,stop)]
    findings = [v for i,v in enumerate(mylist) if mylist.index(v) == i]
    return findings

def get_data_row(start, stop, row):
    mylist = [ws.cell(row = row, column = i).value for i in range(start, stop)]
    values = [v for i,v in enumerate(mylist) if mylist.index(v) == i]
    return values

def write_from_dict(row, col, dict):
    row = row
    col = col
    for key in dict.keys():
        worksheet.write(row, col, key)
        for item in dict[key]:
            worksheet.write(row, col + 1, item)
            row += 1



if __name__ == '__main__':

    mammo_findings_list = get_data(2,10, 11)
    us_findings_list = get_data(10, 17, 11)
    mri_findings_list = get_data(17, 27, 11)

    mammo_params_list = [get_data(2, 5, 14), get_data(5,8,14),
                         get_data(8, 9, 14), get_data(9,10, 14)]
    mammo_data = dict(zip(mammo_findings_list,mammo_params_list))


    mammo = write_from_dict(1, 1, mammo_data)


    shape_val = get_data_row(15, 19, 2)
    margin_val = get_data_row(15, 19, 3)
    density_val = get_data_row(15, 19, 4)

    mass_dict = dict(zip(mammo_params_list[0],shape_val,margin_val,density_val]))
    print(mass_dict) 

    test = write_from_dict(1,2,mass_dict) #eventually didn't work for me now

    workbook.close()

字典输出:

{'Shape': ['Oval, Round', None, 'Irregular'], 'Margin': ['Circumscribed', None, 'Obscured, Microlobulated, Indistinct, Spiculated'], 'Density': ['High density', 'Equal density, Low density', None]}
{'Mass': ['Shape', 'Margin', 'Density'], 'Calcifications': ['Typically benign', 'Suspicious morphology', 'Distribution'], 'Assymetry': ['Assymetry'], 'Lymph nodes': ['Lymph nodes']}

我只需要以某种方式附加/合并这些数据并同时将所有内容写入Excel。

谢谢!

1 个答案:

答案 0 :(得分:1)

如果我们仅限于将术语词典转换为工作表中的相关列。

values_dict = {
'Shape': ['Oval, Round', None, 'Irregular'], 
'Margin': ['Circumscribed', None, 'Obscured, Microlobulated, Indistinct, Spiculated'], 
'Density': ['High density', 'Equal density, Low density', None]
}

如果不是这种情况,则字符串中包含的列表需要先扩展。

最简单的方法是分别处理每个键,值对。对于openpyxl,这将是

for col, (key, values) in enumerate(values_dict.items(), start=1):
    ws.cell(row=1, column=col, value=key) # column title
    for row, v in enumerate(values, start=2):
        ws.cell(row=2, column=col, value=v) # column values

但是这涉及我们在openpyxl中尝试避免的大量行和单元坐标计数。另一种方法是使用zip将值列转换为我们可以添加的行。

from itertools import zip_longest
headers = []
values = []
for k, v in values_dict.items():
    headers.append(k)
    values.append(v)
ws.append(headers)
for row in zip_longest(*values):
    ws.append(row)

当然,如果可以移除中间结构,情况会更好。