首页 > 解决方案 > 如何修复 Python Visio 转换器中的 for 循环

问题描述

我正在尝试运行一个 for 循环,但是当它到达某个日期时让它停止,它会这样做,但是,当我只想要 1 时,它会在彼此之上打印出多个形状。程序以某种方式打印数字形状基于它在 Excel 中的行号。不知道如何解决这个问题,任何帮助将不胜感激。

from PIL import Image, ImageDraw, ImageFont
import win32com.client
from win32com.client import constants as vis
app = win32com.client.gencache.EnsureDispatch( 'Visio.Application' )

current = datetime.datetime(*xlrd.xldate_as_tuple(sheet3.cell_value(7,9), wb.datemode))
currentDate = current.strftime('%m/%d')
dateList = []
for row in range(1,sheet3.nrows):
    if sheet3.cell_value(row,13) == "":
        continue
    date = datetime.datetime(*xlrd.xldate_as_tuple(sheet3.cell_value(row,13), wb.datemode))
    dateList.append(date.strftime('%m/%d'))
    for date in dateList:
        x1 = sheet3.cell_value(row,14)
        x2 = sheet3.cell_value(row,15)
        y1 = sheet3.cell_value(row,16)
        y2 = sheet3.cell_value(row,17)
        borderColor = 0
        borderType = 0
        colorValue = sheet3.cell_value(9,10)
        colorFunc(x1,y1,x2,y2)
        shape.Cells('FillforegndTrans').FormulaU = sheet3.cell_value(7,10)
    if currentDate == date:
        break

标签: pythonexcelwinapivisiowin32com

解决方案


我已经想通了。而不是一个for循环,我只需要在最后声明一个if语句,如果currentDate在dateList中,然后break。

dateList = []
for row in range(1,sheet3.nrows):
    if sheet3.cell_value(row,13) == "":
        continue
    date = datetime.datetime(*xlrd.xldate_as_tuple(sheet3.cell_value(row,13), wb.datemode))
    dateList.append(date.strftime('%m/%d'))
    current = datetime.datetime(*xlrd.xldate_as_tuple(sheet3.cell_value(7,9), wb.datemode))
    currentDate = current.strftime('%m/%d')
    x1 = sheet3.cell_value(row,14)
    x2 = sheet3.cell_value(row,15)
    y1 = sheet3.cell_value(row,16)
    y2 = sheet3.cell_value(row,17)
    borderColor = 0
    borderType = 0
    colorValue = sheet3.cell_value(9,10)
    colorFunc(x1,y1,x2,y2)
    shape.Cells('FillforegndTrans').FormulaU = sheet3.cell_value(7,10)
    if currentDate in dateList:
        break

推荐阅读