简体   繁体   English

OpenPyXl:我可以从一个 Excel 文件中引用数据并根据标识符将值插入到单元格中吗?

[英]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 SKUcell.columncell.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.

相关问题 如何在 excel 的一个单元格中插入 pandas 数据帧(使用 openpyxl),其中的值将用逗号分隔? - How to insert a pandas data frame in one cell in excel (using openpyxl), where the values will be separated with comma? 如何使用 openpyxl 在 XLSX 文件中逐个单元格地插入和格式化值 - How to insert and format values cell by cell in an XLSX file using openpyxl 如何使用openpyxl / python读取Excel文件的合并单元格值? - How to read merged cell values of an excel file with openpyxl / python? 使用 Openpyxl 从 excel 中获取溢出数据的单元格数据 - Get data from cell with spilled data from excel using Openpyxl 如何将每个 excel 文件中的一个单元格添加到新的 excel 文件中? - How can I add only one cell from every excel file to new excel file? 使用openpyxl读写多个excel数据到一个excel文件 - Read and Write multiple excel data into one excel file using openpyxl 使用openpyxl将单元格值列从一个工作簿复制到另一个工作簿 - Copy column of cell values from one workbook to another with openpyxl 如果条件基于使用 Openpyxl 的 excel 中的单元格值 - If condition based on cell value in excel using Openpyxl openpyxl - 我无法将单元格样式复制到另一个中 - openpyxl - I can't copy the cell style in another one xlrd&openpyxl获取错误的单元格值(Excel) - xlrd & openpyxl fetch wrong cell values (Excel)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM