-1
我被困了嘗試使用OpenPyxl和xlsxwriter在Excel擴展行數過了一段時間。追加行的數據和相應的細胞
現在我的文件看起來像:
,我需要追加另一個數據,對應於形狀和移動都記錄下來,所以它看起來像:
我有我的數據['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。
謝謝!
並您的代碼看起來像在看什麼? –
@CharlieClark編輯後。 –
謝謝。你爲什麼使用這兩個庫的任何原因? openpyxl應該能夠做你需要的一切。 –