How to copy a value from an Excel cell and paste it into a new cell at scale

浪子不回头ぞ 提交于 2019-12-24 21:03:13

问题


I'm trying to create code that will search for a color value such as "blue" in a cell and if it is found in that cell, paste that value into another cell, and repeat this for a range of a couple hundred colors.

Once that happens, to do the same process again for the next cell down in the column, and pasting into another cell in the corresponding row, until each populated cell has been searched for all color values.

I've got the code to create an Excel sheet that will pull a few predefined values, but I'm not sure how I will expand the code to include hundreds of different colors.

#Excel Project to fill attributes quickly.

#Importing Modules
import os, openpyxl

#defining variables
wb = openpyxl.Workbook()
sheet = wb['Sheet']

#Asking for your category
x = input('What is your category?')



#Writing header
sheet['A1'] = 'Short Description'
sheet['B1'] = 'Long Description'
sheet['C1'] = 'Item'
sheet['J1'] = 'Color'

#writing attribute values
sheet['A2'] = 'Amazing blue shiny fantastic shirt ee-9003'
sheet['A3'] = 'Red dull jacket'

#Writing values for "Color"

if 'Red' or 'red' in sheet['A2'].value:
    sheet['J2'] = 'Red'
elif 'Orange' or 'orange' in sheet['A2'].value:
    sheet['J2'] = 'Orange'
elif 'Blue' or 'blue' in sheet['A2'].value:
        sheet['J2'] = 'Blue'


if 'Red' or 'red' in sheet['A3'].value:
    sheet['J3'] = 'Red'
elif 'Blue' or 'blue' in sheet['A3'].value:
    sheet['J3'] = 'Blue'


#Saving to your desktop
os.chdir(r'E:\Desktop')
wb.save('Attributes_filled_' + str(x) + '.xlsx')

I expect the code to produce "blue" the first time, and "red" the second time for my code so far, but depending on how I order the code, I get the first if statement result.


回答1:


if 'Red' or 'red' in sheet['A2'].value: is always considered True because non-empty strings are considered True. So in fact it's the same as if True or 'red' in sheet['A2'].value:

From the docs:

By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object. 1 Here are most of the built-in objects considered false:

  • constants defined to be false: None and False.
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: '', (), [], {}, set(), range(0)

any of the following would fix your problem:

if 'red' in sheet['A3'].value.lower():

or

if 'Red' in sheet['A3'].value or 'red' in sheet['A3'].value:

or

if any(color in sheet['A3'].value for color in ('Red', 'red')):

Same apply for all of your if/elif conditions. Also note that if the string is part of bigger string it will still return True, i.e. 'Blue" in "I've Got The Blues" will be evaluated True

EDIT: Because of discussion in the comments, I replace evaluate with considered.



来源:https://stackoverflow.com/questions/53890786/how-to-copy-a-value-from-an-excel-cell-and-paste-it-into-a-new-cell-at-scale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!