我在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()
我遇到的不同结果是:
sht= scBk.Worksheets[1]
-->;"T16_T"
rngtofind.Address
-->;$C$6
,Total
sht= scBk.Worksheets[1]
-->;"T16_T"
rngtofind.Address
-->;$B$3
,(TOTAL)
有办法解决这个问题吗?仅供参考,我希望应用相同的代码,不希望同时维护2个不同版本的代码。你知道吗
其他信息:
编辑: 为了避免在工作表中使用索引,我提出了另一种搜索方法。请参考代码。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐