我有一个脚本,使用下面的简单功能自动创建和发送电子邮件发送电子邮件:
def Emailer(text, subject, recipient):
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.HtmlBody = text
mail.send
Run Code Online (Sandbox Code Playgroud)
但是如何在Outlook窗口中打开此电子邮件以便手动编辑和发送?
理想情况下,我喜欢这样的事情:
def __Emailer(text, subject, recipient, auto=True):
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.HtmlBody = text
if auto:
mail.send
else:
mail.open # or whatever the correct code is
Run Code Online (Sandbox Code Playgroud)
提前致谢
摘要
我有wxPython GUI,允许用户打开文件进行查看.目前我这样做os.startfile().但是,我已经知道这不是最好的方法,所以我希望改进.主要的缺点startfile()是,一旦启动文件,我就无法控制文件.这意味着用户可以将文件保持打开状态,以便其他用户无法使用.
我正在寻找什么
在我的GUI中,可以有子窗口.我通过将GUI对象存储在列表中来跟踪所有这些,然后当父对象关闭时,我只是遍历列表并关闭所有子对象.我想对用户选择的任何文件执行相同操作.如何启动文件并保留python对象,以便我可以在命令中关闭它?提前致谢
我的解决方案的梦想
进展到目前为止
这是我计划使用的框架.最重要的位是run()和end()该功能FileThread类,因为这是在解决方案会去.
import wx
from wx.lib.scrolledpanel import ScrolledPanel
import threading
import os
class GUI(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Hey, a GUI!', size=(300,300))
self.panel = ScrolledPanel(parent=self, id=-1)
self.panel.SetupScrolling()
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.openFiles = []
self.openBtn = wx.Button(self.panel, -1, "Open a File")
self.pollBtn = wx.Button(self.panel, -1, "Poll")
self.Bind(wx.EVT_BUTTON, self.OnOpen, self.openBtn)
self.Bind(wx.EVT_BUTTON, self.OnPoll, self.pollBtn)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add((20,20), 1)
vbox.Add(self.openBtn)
vbox.Add((20,20), 1)
vbox.Add(self.pollBtn)
vbox.Add((20,20), …Run Code Online (Sandbox Code Playgroud) 我注意到一些奇怪的行为与今天玩弄next()和readline().似乎两个函数都产生相同的结果(这是我所期望的).但是,当我混合它们时,我会得到一个ValueError.这是我做的:
>>> f = open("text.txt", 'r')
>>> f.readline()
'line 0\n'
>>> f.readline()
'line 1\n'
>>> f.readline()
'line 2\n'
>>> f.next()
'line 3\n'
>>> f.next()
'line 4\n'
>>> f.readline()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Mixing iteration and read methods would lose data
>>>
>>> f = open("text.txt", 'r')
>>> f.next()
'line 0\n'
>>> f.next()
'line 1\n'
>>> f.next()
'line 2\n'
>>> f.readline()
Traceback (most recent call last):
File …Run Code Online (Sandbox Code Playgroud) 我正在制作一个实用程序脚本来完成一大堆工作.我想做的一件事就是旋转显示器; 我有多个显示器,我想要主旋转.我知道这种事情通常都可以解决win32api,我在那里发现了一些似乎有用的功能,但我正在努力实现.
此行与下一行之间的所有内容都已过期,请参阅下面的第二行,了解解决方案尝试的最新说明
在将我的脸埋进文档之后,我恐怕我还没有太多想法如何向前推进它可能涉及的范围win32api.ChangeDisplaySettingsEx().我知道我需要给该函数一个指向DEVMODE对象的指针(甚至不确定如何在python中执行C指针),我认为我可以从中获取win32api.EnumDisplaySettingsEx().所以,如果我尝试,
>>> import win32api as win32
>>> a = win32.EnumDisplayDevices()
>>> type(a)
Run Code Online (Sandbox Code Playgroud)
我应该得到一些涉及DEVMODE指针或其他的东西,但我得到了
>>> type(a)
<type 'PyDISPLAY_DEVICE'>
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么做,但我认为这是结构
那么,我如何得到一个DEVMODE可以给予的ojbect,ChangeDisplaySettingsEx()以便我可以旋转我的一个显示器?提前致谢.
我在Windows 7上运行Python 2.7
编辑: 如果我实际使用正确的功能,它仍然无法正常工作.这可能是Python模块不完整吗?
>>> a = win32.EnumDisplaySettings()
>>> type(a)
<type 'PyDEVMODEA'>
>>> a.dmSize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'PyDEVMODEA' object has no attribute 'dmSize'
>>> a.dmScale
Traceback (most recent call last):
File "<stdin>", line …Run Code Online (Sandbox Code Playgroud) 背景资料
我有一个Python脚本,它使用docx模块生成word文档.这些文档基于日志生成,然后打印并存储为记录.但是,可以追溯编辑日志,因此需要修改文档记录,并且必须跟踪这些修订.我实际上并没有修改文档,而是生成一个新的文档,显示当前日志中的内容与日志中的内容之间的差异(在修改后的文件打印后更新日志).发生修订时,我的脚本使用diff_match_patch以下函数生成已更改内容的标记:
def revFinder(str1,str2):
dmp = dmp_module.diff_match_patch()
diffs = dmp.diff_main(str1,str2)
paratext = []
for diff in diffs:
paratext.append((diff[1], '' if diff[0] == 0 else ('s' if diff[0] == -1 else 'b')))
return paratext
Run Code Online (Sandbox Code Playgroud)
docx 如果需要逐字格式化,可以将文本作为字符串或元组,所以[参见"需要注意的事项"中的第二篇子句]
[("Hello, ", ''), ("my name ", 'b'), ("is Brad", 's')]
Run Code Online (Sandbox Code Playgroud)
产生
你好我的名字
是布拉德
问题
diff_match_patch是一个非常有效的代码,它找到两个文本之间的差异.不幸的是,它有点太高效,所以取而代之的redundant是dune结果
redunante
这很丑陋,但对单个单词来说很好.但是,如果整个段落被替换,结果将完全不可读.那不行.
之前我通过将所有文本折叠成一个段落来解决这个问题,但这不太理想,因为它变得非常混乱并且仍然非常丑陋.
迄今为止的解决方案
我有一个创建修订文档的功能.这个函数传递了一个像这样设置的元组列表:
[(fieldName, original, revised)]
Run Code Online (Sandbox Code Playgroud)
所以文档设置为
Orignial fieldName (With Markup)
result of revFinder diffing …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用SharePoint包编写脚本来访问我公司的SharePoint上的文件.教程说明
首先,您需要创建一个
SharePointSite对象.我们假设您正在使用基本身份验证; 如果你不是,你需要自己创建一个合适的urllib2开启者.
然而,经过几次尝试,我得出结论,基本的auth是不够的.在研究如何使其工作时,我发现了这篇文章,它很好地概述了一般的身份验证方案.我正在努力解决的是在Python中实现这一点.
我设法劫持了SharePoint模块中的基本身份验证.为此,我在链接的文章中使用了XML消息,并使用它来替换SharePoint模块生成的XML.在进行了一些其他更改后,我现在收到链接文章的步骤2中所述的令牌.
现在,在步骤3中,我需要使用POST将该令牌发送到SharePoint.以下是它应该是什么样子的样本:
POST http://yourdomain.sharepoint.com/_forms/default.aspx?wa=wsignin1.0 HTTP/1.1
Host: yourdomain.sharepoint.com
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)
Content-Length: [calculate]
t=EwBgAk6hB....abbreviated
Run Code Online (Sandbox Code Playgroud)
我目前使用以下代码生成我的POST.在其他一些问题的指导下,我省略了content-length标题,因为应该自动计算.我不确定将令牌放在哪里,所以我把它推进去了data.
headers = {
'Host': 'mydomain.sharepoint.com',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)'
}
data = {'t':'{}'.format(token[2:])}
data = urlencode(data)
postURL = "https://mydomain.sharepoint.com/_forms/default.aspx?wa=wsignin1.0"
req = Request(postURL, data, headers)
response = urlopen(req)
Run Code Online (Sandbox Code Playgroud)
但是,这会产生以下错误消息:
urllib2.HTTPError: HTTP Error 302: The …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下其他用户发布的作为不同问题的答案:
>>> # import the hash algorithm
>>> from passlib.hash import sha256_crypt
>>> # generate new salt, and hash a password
>>> hash = sha256_crypt.encrypt("toomanysecrets")
>>> hash
Run Code Online (Sandbox Code Playgroud)
但是当我输入时,from passlib.hash import sha256_crypt我收到以下错误:
Traceback (most recent call last): File "<stdin>", line 1, in
<module> ImportError: No module named passlib.hash
>>>
Run Code Online (Sandbox Code Playgroud)
我已经完成了pip install passlib.有任何想法吗?
运行结果pip install passlib:
Downloading/unpacking passlib Downloading passlib-1.6.2.tar.gz (408kB): 408kB downloaded Running setup.py egg_info for package passlib
Installing collected packages: passlib Running setup.py install …Run Code Online (Sandbox Code Playgroud) 我正在编写需要能够跟踪修订的脚本。总体思路是给它一个元组列表,其中第一个条目是字段的名称(即“标题”或“描述”等),第二个条目是该字段的第一个版本,第三个条目是修订版。所以像这样:
[("Title", "The first version of the title", "The second version of the title")]
Run Code Online (Sandbox Code Playgroud)
现在,使用python docx“我希望我的脚本”创建一个 Word 文件,该文件将显示原始版本以及以粗体显示更改的新版本。例子:
原标题:
这是标题的第一个版本
修改后的标题:
这是标题的第二个版本
完成此操作的方法python docx是创建一个元组列表,其中第一个条目是文本,第二个条目是格式。所以创建修改后的标题的方法是这样的:
paratext = [("This is the ", ''),("second",'b'),(" version of the title",'')]
Run Code Online (Sandbox Code Playgroud)
最近发现difflib我认为这将是一项非常简单的任务。事实上,对于简单的单词替换(例如上面的示例),可以使用以下函数来完成:
def revFinder(str1,str2):
s = difflib.SequenceMatcher(None, str1, str2)
matches = s.get_matching_blocks()[:-1]
paratext = []
for i in range(len(matches)):
print "------"
print str1[matches[i][0]:matches[i][0]+matches[i][2]]
print str2[matches[i][1]:matches[i][1]+matches[i][2]]
paratext.append((str2[matches[i][1]:matches[i][1]+matches[i][2]],''))
if i != len(matches)-1:
print ""
print str1[matches[i][0]+matches[i][2]:matches[i+1][0]]
print str2[matches[i][1]+matches[i][2]:matches[i+1][1]]
if len(str2[matches[i][1]+matches[i][2]:matches[i+1][1]]) > …Run Code Online (Sandbox Code Playgroud) 假设我有一个功能如下:
def foo(**kwargs):
print kwargs
Run Code Online (Sandbox Code Playgroud)
然后像这样调用函数,我得到了这个方便的小字典kwargs。
>>> foo(a = 5, b = 7)
{'a': 5, 'b': 7}
Run Code Online (Sandbox Code Playgroud)
我想直接对我从命令行调用的脚本执行此操作。所以输入这个:
python script.py a = 5 b = 7
Run Code Online (Sandbox Code Playgroud)
将创建与上述示例类似的字典。这能做到吗?
这是我到目前为止所拥有的:
import sys
kwargs_raw = sys.argv[1:]
kwargs = {key:val for key, val in zip(kwargs_raw[::3], kwargs_raw[1::3])}
print kwargs
Run Code Online (Sandbox Code Playgroud)
这是产生的结果:
Y:\...\Python>python test.py a = 5 b = 7
{'a': '5', 'b': '7'}
Run Code Online (Sandbox Code Playgroud)
所以你可能想知道为什么这还不够好
a或b其他内容,则将不起作用。我以前ast.literal_eval()在这里见过,但我不知道如何让它工作。我的两次尝试都失败了:
>>> ast.literal_eval("a = 5")
Traceback (most recent call last): …Run Code Online (Sandbox Code Playgroud) 我理解通过使用内置方法将int转换为字符串很容易str().然而,究竟发生了什么?我理解它可能指向__str__int对象的
方法,但它如何计算"非正式"字符串表示?试着看源头而没找到领先; 任何帮助赞赏.
python ×10
difflib ×1
email ×1
office365 ×1
outlook ×1
popen ×1
python-2.7 ×1
sharepoint ×1
shell ×1
subprocess ×1
urllib2 ×1
winapi ×1
windows ×1