使用python将文件上传到sharepoint时出错

时间:2014-07-28 23:37:26

标签: python sharepoint xmlhttprequest

我正在尝试使用python脚本将文件从桌面上传到SharePoint。我收到错误" xml.sax._exceptions.SAXparseException找不到元素" ...请建议一个解决方案,因为我对python很新,到目前为止我所有的尝试都失败了。该脚本如下所示,

#!/usr/bin/python2.4

import datetime as dt
import sys
from suds.transport.https import WindowsHttpAuthenticated
from suds.sax.element import Element
from suds.sax.element import Attribute
from suds import client
from ntlm import HTTPNtlmAuthHandler
import urllib2
import os.path

FOLDER = dt.date.today().strftime("%Y-%m-%d")  #folder name that will be created
FNAME = "Testfile"                          #file name to upload
SITE = "My sharepoint path"
FURL = "%s/Reports/%s/%s" % (SITE,FOLDER,os.path.basename(FNAME))
USER = "Domain\\UserID"   # AD user name
PASS = "Password"

def main():
 wss_lists = client.Client("%s/_vti_bin/lists.asmx?WSDL" %                         SITE,transport=WindowsHttpAuthenticated(username=USER,password=PASS))
 wss_dws = client.Client("%s/_vti_bin/dws.asmx?WSDL" % SITE,transport=WindowsHttpAuthenticated(username=USER,password=PASS))
 wss_dws.service.CreateFolder("Reports/%s" % FOLDER)
  print uploadReport(FURL,FNAME)


def uploadReport(furl,fname):
  pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
  pm.add_password(None,SITE,USER,PASS)
  op = urllib2.build_opener(HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(pm))
  import pdb;pdb.set_trace()
  fh = open(fname)
  data = fh.read()
  fh.close()
  req = urllib2.Request(furl,data=data)
  req.get_method = lambda: 'PUT'
  req.add_header('Content-Type','txt')
  r = op.open(req)enter code here
  return r.read()

if __name__=="__main__": main()

2 个答案:

答案 0 :(得分:0)

代码似乎建议它只上传从磁盘读取的文件,因此您尝试上传的XML可能格式不正确。我会先检查一下。

另一个问题是2.4真的很老了。如果可以,请安装2.7,以避免旧的错误咬你。

然后你也可以做很好的事情:

with open('Testfile') as fileobj:
   content = fileobj.read()

执行后离开with阻止fileobj将自动关闭。

答案 1 :(得分:0)

我找到了一个解决方法....我正在创建一个"地图驱动器" ...创建地图驱动器后,只需复制并粘贴,如果你将文件复制到地图驱动器然后它会自动反映在sharepoint上..

您可以手动创建地图驱动器。或者,要使用脚本创建Map驱动器,请参阅以下链接: -

What is the best way to map windows drives using Python?