我试图弄清楚如何在Excel工作表中读取数据,并在Excel工作表中(而不是在Python控制台中)将结果打印到右侧一列。下面是我正在测试的代码。它似乎可以正常工作,但是只在控制台中显示结果,我真的想将结果写入Excel中的相邻单元格(列B)。
<Grid Width="128" Height="128" Panel.ZIndex="1">
<Ellipse Fill="Aqua"></Ellipse>
<Path Fill="LightBlue" Stroke="Black" Name="TriOne">
<Path.Data>
<PathGeometry>
<PathFigure x:Name="fig" StartPoint="44, 32" IsClosed="True">
<LineSegment Point="44, 64"/>
<LineSegment x:Name="middle" Point="100, 64"/>
<LineSegment Point="100, 64"/>
</PathFigure>
</PathGeometry>
</Path.Data>
<Path.Triggers>
<EventTrigger RoutedEvent="Polygon.MouseUp">
<BeginStoryboard>
<Storyboard>
<PointAnimation
Storyboard.TargetName="fig"
Storyboard.TargetProperty="StartPoint"
From="44,32" To="32,32"
/>
<PointAnimation
Storyboard.TargetName="middle"
Storyboard.TargetProperty="Point"
From="100, 64" To="90, 54"
/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Path.Triggers>
</Path>
<Polygon Fill="LightBlue" Stroke="Black" Name="TriTwo" >
<Polygon.Points>
<Point X="44" Y="96"></Point>
<Point X="44" Y="64"></Point>
<Point X="100" Y="64"></Point>
</Polygon.Points>
</Polygon>
</Grid>
答案 0 :(得分:0)
使用cell.offset(column=1)
获取下一个单元格。
答案 1 :(得分:0)
这很容易:
如果已确定要写入B列:
for rowno,row in enumerate(ws.iter_rows('A{}:A{}'.format(ws.min_row,ws.max_row)),start=1):
for cell in row:
if isinstance(cell.value, str):
print('string')
ws.cell(row=rowno,column=2).value = str("string")
elif isinstance(cell.value, int):
print('integer')
ws.cell(row=rowno,column=2).value = str("integer")
elif isinstance(cell.value, float):
print('float')
ws.cell(row=rowno,column=2).value = str("float")
wb.save('C:\\Users\\Excel\\Desktop\\Book1.xlsx')
更多通用答案:
from openpyxl.utils import get_column_letter
from openpyxl.utils import coordinate_from_string, column_index_from_string
for rowno,row in enumerate(ws.iter_rows('A{}:A{}'.format(ws.min_row,ws.max_row)),start=1):
for cell in row:
if isinstance(cell.value, str):
xy = coordinate_from_string(cell.coordinate)
colno = column_index_from_string[0]
ws.cell(row=rowno, column=colno+1).value = str("string")
elif isinstance(cell.value, int):
xy = coordinate_from_string(cell.coordinate)
colno = column_index_from_string[0]
ws.cell(row=rowno, column=colno+1).value = str("string")
elif isinstance(cell.value, float):
xy = coordinate_from_string(cell.coordinate)
colno = column_index_from_string[0]
ws.cell(row=rowno, column=colno+1).value = str("string")
wb.save('C:\\Users\\Excel\\Desktop\\Book1.xlsx')