问题:使用openpyxl-python在Excel文件的每一列或每行中添加“(C)”或“(S)”。
患者记录或练习列表的示例 对于每个(物理治疗)患者,该列表将有数十个(如果不是数百个)练习。
我想在练习中添加一个指标: (C)为Cardio (S)为Strenght 注意:会有更多指标(~20)。我们有数以千计的患者记录文件没有任何分类。
例如,我们想要为运行添加(C):
这就是我设置它的方式: 注意:我不允许在工作计算机上安装任何软件包。但是,我使用的是openpyxl,因为它已经安装在系统中。
## Load your work book into a global variable
wb = openpyxl.load_workbook('ExcersiceList.xlsx')
## Read the sheets name for your entire workbook
wb.get_sheet_names()
## Create a variable for each sheet in the work book, to manipulate each sheet
sheet1 = wb.get_sheet_by_name('Sheet1')
理论上,我想对每一行和每列进行此操作
## To add the strings to existing values in a cell, use the following
varB2 = sheet1[‘A2’].value ## assign the value of A2 to varA2
sheet1[‘A2’] = ‘(C) ’ + varA2 ## this combines the value of A2 with (U)
sheet1[‘A2’].value ## you will notice a value change
wb.save(‘ExcersiceList.xlsx’)
我尝试至少为行做一个循环:
##loop through sheet1 max row
for row in range(1, sheet1.max_row+1):
st1 = '(c) ' + str(row)
print st1enter code here
wb.save(‘yourFileName.xlsx’)
但是st1 =它只是被分配而不是写回excel文件。
我提前感谢您的时间和指导。如果您需要更多信息,请与我们联系。但是,请理解我无法透露真实的患者数据。
答案 0 :(得分:1)
您可以使用cell
功能,请参阅文档中的Accessing one cell。
例如,假设您已将表单加载到sheet1
中,详细信息位于第一列:
for row in range(1, sheet1.max_row+1):
cell = sheet1.cell(row=row, column=1)
if cell.value is not None:
cell.value = "(c) " + cell.value
wb.save('test2.xlsx')