Python在本地PC和Windows上运行时会产生不同的结果

2024-10-04 11:35:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我在PC和Windows服务器上运行相同的python脚本。指向相同的项目解释器(C:\Users\<username>\AppData\Local\Programs\Python\Python36-32\python.exe),再加上使用相同的版本和包。我的代码在本地PC机上运行得很好,但在服务器上会产生不同的结果。代码如下:

import win32com.client as xl
import xlwings.constants

bookName = r'C:\Users\<username>\Desktop\Python_Projects\test_case.xlsx'
# in server the path is 'D:\Programmer\Python\test_case.xlsx'

sf = xl.DispatchEx("Excel.Application")
scBk = sf.Workbooks.Open(bookName)
#to find sheet "T16_T"
#sht= scBk.Worksheets[1] -->old code
#edit with this new code
for list_sht in scBk.Sheets:
   sht= scBk.Worksheets[list_sht.Name]; break
sf.Visible = True

rngtofind= sht.Cells.Find(What='Total', LookAt=xlwings.constants.LookAt.xlWhole, LookIn=xlwings.constants.FindLookIn.xlFormulas, MatchCase = False)
If rngtofind is not None:
   print(rngtofind.Address)

scBk.Close()
sf.Quit()

我遇到的不同结果是:

  1. 本地PC:
    • sht= scBk.Worksheets[1]-->;"T16_T"
    • rngtofind.Address-->;$C$6Total
  2. Windows server,修改代码后:
    • sht= scBk.Worksheets[1]-->;"T16_T"
    • rngtofind.Address-->;$B$3(TOTAL)

有办法解决这个问题吗?仅供参考,我希望应用相同的代码,不希望同时维护2个不同版本的代码。你知道吗

其他信息:

  • Python 3.6版
  • 皮查姆
  • Windows 7-64位
  • Windows Server 2016-64位 Sample case "T16_T"Sample case "T16_M"

编辑: 为了避免在工作表中使用索引,我提出了另一种搜索方法。请参考代码。你知道吗


Tags: 代码gt服务器addresswindowssfxlwingsusers