Domanda

I have an excel spreadsheet. I'd like to iterate through column A of that spreadsheet and write to column B using the value from column A in a string. I've been successful with copying & pasting in Excel using Python and have a couple scripts set up and now this one is presenting a hurtle. EDIT: I need it to stop once there are no more values in column A.

import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = 1;
wb = excel.Workbooks.Open("C:\file.xls");
ws = wb.Worksheets("Sheet1")

I've written what the code should look like below, but struggle with Python (beginner) and am not sure how to set it up:

starting read cell = A1
starting write cell = B1
for cell in column A:
    ws.Range("B1").Value = str("W"&TEXT(A1,"00000"))

So, if my excel spreadsheet looks like this:

A B
1
2

Then the result would be:

A B
1 W00001
2 W00002

(Notice I'm using excel formulas to write here).

Any suggestions?? Thanks everyone!

È stato utile?

Soluzione

Try:

col = ws.Range("A1:A100")
for cell in col:
    cell.Offset(1,2).Value = "W0000%s" % cell.Value
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top