【发布时间】:2021-11-07 20:30:19
【问题描述】:
我正在运行一个 python 代码来分隔 Excel 中的文本,例如,我们在一个单元格中(尺寸:15 毫米)代码将采用符号“:”之前的单词“尺寸”并将其添加为一列名称然后取“15mm”并将其添加为值。
问题是代码仅在我指定单元格(例如 A4)时才有效。您如何请求遍历 A 列的所有单元格?
import openpyxl
book = openpyxl.load_workbook('C:/Users/zalka/Desktop/Python project/new13.xlsx')
sheet = book.active
U4 = sheet["A4"]
Column_base = 3
i = 0
a = sheet.cell(row=3, column=1)
tech_details = U4.value.splitlines()
print(U4.value.splitlines())
for tech_detail in tech_details:
print(tech_detail)
print(tech_detail.index(":"))
s = tech_detail.index(":")
name = tech_detail[:s]
print(name)
value = tech_detail[s+1:]
print(value)
sheet.cell(row=3, column=i+Column_base).value = name
sheet.cell(row=4, column=i+Column_base).value = value
i=i+1
book.save('C:/Users/zalka/Desktop/Python project/write2cell2.xlsx')
【问题讨论】:
-
下一个单元格的数据在哪里,例如A5 去?
-
你看过 openpyxl 文档吗?