简体   繁体   English

遍历Excel的A列,使用Python win32写入B列

[英]Iterate thru Excel column A , write to column B using Python win32

I have an excel spreadsheet. 我有一个Excel电子表格。 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. 我想遍历该电子表格的A列,并使用字符串中A列的值写入B列。 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. 我已经成功地使用Python在Excel中复制和粘贴,并且设置了两个脚本,现在这个脚本很麻烦。 EDIT: I need it to stop once there are no more values in column A. 编辑:一旦列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: 我已经编写了下面的代码,但是与Python(初学者)很不一样,并且不确定如何设置它:

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: 因此,如果我的Excel电子表格如下所示:

A B
1
2

Then the result would be: 那么结果将是:

A B
1 W00001
2 W00002

(Notice I'm using excel formulas to write here). (注意,我在这里使用的是excel公式)。

Any suggestions?? 有什么建议么?? Thanks everyone! 感谢大家!

Try: 尝试:

col = ws.Range("A1:A100")
for cell in col:
    cell.Offset(1,2).Value = "W0000%s" % cell.Value

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM