简体   繁体   English

如何使用 Python 和 openpyxl 仅在 Excel 中粘贴值

[英]How to paste values only in Excel using Python and openpyxl

I have an Excel worksheet.我有一个 Excel 工作表。 In column J i have some some source data which i used to make calculations in column K. Column K has the values I need, but when i click on a cell the formula shows up.在 J 列中,我有一些源数据,用于在 K 列中进行计算。K 列具有我需要的值,但是当我单击一个单元格时,会显示公式。 I only want the values from column K, not the formula.我只想要 K 列的值,而不是公式。 I read somewhere that i need to set data only=True, which I have done.我在某处读到我需要设置 data only=True,我已经这样做了。 I then pasted data from Column K to Column L(with the intention of later deleting Columns J and K).然后,我将 K 列中的数据粘贴到 L 列(目的是稍后删除 J 列和 K 列)。 I thought that Column L will have only the values from K but if i click on a cell, the formula still shows up.我认为 L 列将只有 K 中的值,但如果我单击一个单元格,公式仍然会显示。 How do I simply paste values only from one column to another?如何仅将值从一列粘贴到另一列?

import openpyxl
wb = openpyxl.load_workbook('edited4.xlsx', data_only=True)
sheet = wb['Sheet1']

last_row = 100  
for i in range(2, last_row):
    cell = "K" + str(i)
    a_cell = "J" + str(i)
    sheet[cell] = '=IF(' + a_cell + '="R","Yes","No")'

rangeselected = []
for i in range (1, 100,1):
    rangeselected.append(sheet.cell(row = i, column = 11).value)
for i in range (1, 1000,1):
   sheet.cell(row=i, column=12).value = rangeselected[i-1]

wb.save('edited4.xlsx')

It's been a while since I've used openpyxl.自从我使用 openpyxl 已经有一段时间了。 But:但:

Openpyxl doesn't run an Excel formula. Openpyxl 不运行 Excel 公式。 It reads either the formula string or the results of the last calculation run by Excel*.它读取公式字符串或 Excel* 运行的最后一次计算的结果。 This means that if a calculation is created outside of Excel, and the file has never been open by Excel, then only the formula will be available.这意味着,如果在 Excel 之外创建计算,并且 Excel 从未打开过该文件,则只有公式可用。 Unless you need to display (for historical purposes, etc.) what the formula is, you should do the calculation in Python - which will be faster and more efficient anyway.除非您需要显示(出于历史目的等)公式是什么,否则您应该在 Python 中进行计算——无论如何这将更快、更有效。

* When I say Excel, I also include any Excel-like spreadsheet that will cache the results of the last run. * 当我说 Excel 时,我还包括任何将缓存上次运行结果的类似 Excel 的电子表格。

Try this (adjust column numbers as desired):试试这个(根据需要调整列号):

import openpyxl
wb = openpyxl.load_workbook('edited4.xlsx', data_only=True)
sheet = wb['Sheet1']

last_row = 100  
data_column = 11
test_column = 12
result_column = 13

for i in range(2, last_row):
    if sheet.cell(row=i, column=test_column).value == "R":
        sheet.cell(row=i, column=result_column).value = "Yes"
    else:
        sheet.cell(row=i, column=result_column).value = "No"

wb.save('edited4.xlsx')

If you have a well-formed data sheet, you could probably shorten this by another step or two by using enumerate() and Worksheet.iter_rows() but I'll leave that to your imagination.如果您有一个格式良好的数据表,您可能可以通过使用 enumerate() 和 Worksheet.iter_rows() 将其缩短一两步,但我将把它留给您想象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 openpyxl 将一个 excel 文件的列值与 Python 中另一个 excel 文件的列值进行比较? - How to compare column values of one excel file to the column values of another excel file in Python using openpyxl? 如何使用openpyxl python在excel中插入复选框? - How to insert checkbox in excel using openpyxl python ? 如何使用 openpyxl python 库或任何其他 python 模块从 excel 列值中获取字体颜色 - How to fetch font color from excel column values using openpyxl python library or any other python module 使用 Excel(OpenPyXl) 选择下拉值 - Selenium-Python - Selecting dropdown values using Excel(OpenPyXl) - Selenium-Python 如何在不使用 pandas、openpyxl 等的情况下读取 excel 文件中的值...在 python 中? - How to read values in excel file without using pandas, openpyxl etc...in python?? 如何使用excel openpyxl python仅获取公式的输出 - How to get only the output of a formula with excel openpyxl python Openpyxl - 如何在 Python 中从 Excel 文件中仅读取一列? - Openpyxl - How to read only one column from Excel file in Python? Python使用openpyxl操纵Excel - Python using openpyxl manipulate Excel 如何使用openpyxl / python读取Excel文件的合并单元格值? - How to read merged cell values of an excel file with openpyxl / python? 我尝试使用 python openpyxl 在 excel 中创建多个工作表以在循环中存储不同的值,但它只创建了一张工作表 - i tried to create multiple sheets in excel using python openpyxl to store different values in loop, but it creates only one sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM