[英]OpenPyXl: Can I reference data from one Excel file and insert values into a cell based on an identifier?
Is it possible to get the SKUs — and other information — from one Excel file and reference it in another below an identifier: SKU, Name, etc.?是否可以从一个 Excel 文件中获取 SKU 和其他信息,并在另一个标识符下方引用它:SKU、名称等?
I have one Excel file with various sheets which acts like a template.我有一个 Excel 文件,其中包含像模板一样的各种工作表。 In each sheet the same, or very similar, identifiers are found eg
Product SKU
.在每张表中都可以找到相同或非常相似的标识符,例如
Product SKU
。
My current idea is that once I have the cell.column
and cell.row
for Product SKU
I can store that in variable, respectively, and increment the cell.row
by +1
so I can load the information from another sheet below Product SKU
.我目前的想法是,一旦我有了
Product SKU
的cell.column
和cell.row
,我就可以分别将其存储在变量中,并将cell.row
增加+1
,这样我就可以从Product SKU
下面的另一张表中加载信息。 The end result would look like this in Excel.最终结果在 Excel 中将如下所示。 I would apply the logic above to all other information I want to load.
我会将上述逻辑应用于我想要加载的所有其他信息。
+-------------+----------------+
| Product Sku | Product Name |
+-------------+----------------+
| 12345678 | Some Product |
+-------------+----------------+
The idea I am trying to produce.我试图产生的想法。 If there is a better way, please do let me know.
如果有更好的方法,请告诉我。
from openpyxl import load_workbook
wb = load_workbook(filename = "product_import_tmp.xlsx")
inventory_1_sheet = wb["Inventory 1"]
for row in inventory_1_sheet.iter_rows(values_only=False):
for cell in row:
if cell.value == "Product SKU":
sku_y = cell.column
sku_x = cell.row + 1 # The Product SKU information would go here
It would be better if you can provide what your excel files look like.如果你能提供你的 excel 文件的样子会更好。 I would suggest pandas instead of openpyxl if there is complex manipulation to excel.
如果对 excel 有复杂的操作,我建议使用 pandas 而不是 openpyxl。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.