使用来自 python 的 openpyxl 更新行和列
Update rows and column using openpyxl from python
问题: 使用 openpyxl 在 excel 文件的每一列或每一行中添加“(C)”或“(S)” - python.
患者记录或练习列表示例
该列表将为每个(物理治疗)患者提供数十个甚至数百个练习。
- 重复练习
- 运行 5 分钟
- 步行 10 分钟
- 深蹲 3x12
- 卷发 3x12
- .. .....
我想在练习中添加一个指标:
(C) 有氧运动
(S) 代表强度
注意:会有更多指标(~20)。我们有数千份没有任何分类的患者记录文件。
例如,我们要为 运行 添加 (C):
- A B
- 重复练习
- (C) 运行 时间分钟
- (C) 步行时间分钟
- (S) 深蹲 3x12
- (S) 弯举 3x12
- ..... .....
注意:由于 table 的限制,A1 是练习 B1 是重复,A2 是 运行,B2 是时间分钟,
我是这样设置的:
注意:我不允许在工作计算机上安装任何软件包。但是,我使用的是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 文件。
非常感谢您提前抽出宝贵的时间和指导。如果您需要更多信息,请告诉我。但是,请理解我不能透露真实的患者数据。
您可以使用 cell
功能,请参阅文档中的 Accessing one cell。
例如,假设您已将 sheet 加载到 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')
问题: 使用 openpyxl 在 excel 文件的每一列或每一行中添加“(C)”或“(S)” - python.
患者记录或练习列表示例 该列表将为每个(物理治疗)患者提供数十个甚至数百个练习。
- 重复练习
- 运行 5 分钟
- 步行 10 分钟
- 深蹲 3x12
- 卷发 3x12
- .. .....
我想在练习中添加一个指标: (C) 有氧运动 (S) 代表强度 注意:会有更多指标(~20)。我们有数千份没有任何分类的患者记录文件。
例如,我们要为 运行 添加 (C):
- A B
- 重复练习
- (C) 运行 时间分钟
- (C) 步行时间分钟
- (S) 深蹲 3x12
- (S) 弯举 3x12
- ..... ..... 注意:由于 table 的限制,A1 是练习 B1 是重复,A2 是 运行,B2 是时间分钟,
我是这样设置的: 注意:我不允许在工作计算机上安装任何软件包。但是,我使用的是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 文件。
非常感谢您提前抽出宝贵的时间和指导。如果您需要更多信息,请告诉我。但是,请理解我不能透露真实的患者数据。
您可以使用 cell
功能,请参阅文档中的 Accessing one cell。
例如,假设您已将 sheet 加载到 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')