i'm not a expert in python and it's my fist post in stack overflow.
Maybe you can help me
I try to use python instead of vba to create document for a specific application.
This application is used to configure Programmable RTUs
It is possible to automate the creation of documents via excel by activating in excel a reference to this specific application.
This is a exemple to add a specific tag to a exsisting timer in a document with VBA in Excel
Sub timer()
Dim TWsdoc As TWinSoft.Document
Dim MyTag As TWinSoft.Tagname
Dim file_path As String
file_path = ThisWorkbook.Path
Set TWsdoc = GetObject("/RTU:MS32S2", "TWinSoft.Document")
Set TWsdoc = GetObject(file_path & "\test.tws")
Dim MyTimer As TWinSoft.OSCTimer
Set MyTimer = TWsdoc.OSCTimers("Timer_1")
'add Status Tag in Timer_1
Set MyTimer.Status = TWsdoc.Tagnames("Timer_1_State")
TWsdoc.Save 'save the document
Set TWsdoc = Nothing
End Sub
This is the python code i use to:
Creat a tag (status) with soms parameter
creat a timer (Timer 1)
try to associate timer status to a timer
and save the document
I use Visual Studio Code
THE problem, i can not associate the tag Timer_1_Status to the Timer_1 like in vba
I always have this error
Une exception s'est produite : AttributeError Property
'.Status' can not be set.
from multiprocessing import context
import win32com.client #need pywin32 (pip install pywin32)
import os, os.path
file = "\\test.tws"
file_path = os.path.dirname(os.path.abspath(__file__))
file_path = file_path + file
twdoc = win32com.client.Dispatch('Twinsoft.Document')
#-----------Creat Timer Status Tag-----------------
tagTimer1Sta=twdoc.AddTag('Timer_1_Status','DIV')
tagTimer1Sta.comment = 'Timer 1 Status'
tagTimer1Sta.ModbusAddress = 5000
#-----------Creat Timer-----------------
twdoc.AddOSCTimer("Timer_1")
#----------Associate Timer Status Tag to Timer
mytimer = twdoc.OSCTimers("Timer_1")
mytimer.Status = twdoc.Tagnames(tagTimer1Sta)
#-----------Save the created tws Document-----------------------
twdoc.SaveAs(file_path)
I thank you in advance for your help and I apologize for the bad English that I write, I am French.....
Yvan
Related
I need to use python 3 to create a shortcut to go to some site. The file must have the extension ".url". How can I do this?
I found some code but it doesn't work for me
bmurl = card.url # some link
bmpath = path_to_card + "link.url" # path to file
ws = win32com.client.Dispatch("wscript.shell")
scut = ws.CreateShortcut(bmpath)
scut.TargetPath = bmurl # in this place I got an error
scut.Save()
I have written the following VBA code to automate SAS processes.
Rem Start the SAS server
Dim SASws As SAS.Workspace
Dim SASwsm As New SASWorkspaceManager.WorkspaceManager
Dim strError As String
Set SASws = SASwsm.Workspaces.CreateWorkspaceByServer _
("MySAS", VisibilityProcess, Nothing, "", "", strError)
Dim code_location As String, code_name As String, param_str As String
Dim param_flag As Boolean
code_location = "file:" & ThisWorkbook.Sheets("Control").Range("B2").Value
code_name = ThisWorkbook.Sheets("Control").Range("C2").Value
param_flag = ThisWorkbook.Sheets("Control").Range("C4").Value
If param_flag = True Then
param_str = ThisWorkbook.Sheets("Control").Range("D5").Value
Else: param_str = "ds=Sasuser.Export_output"
End If
Rem Run the stored process
Dim SASproc As SAS.StoredProcessService
Set SASproc = SASws.LanguageService.StoredProcessService
SASproc.Repository = code_location
SASproc.Execute code_name, "ds=Sasuser.Export_output"
'SASproc.Repository = "file:C:\Duopa_Repository\SAS_Codes\Weekly"
'SASproc.Execute "weekly_refresh_run.sas", "ds=Sasuser.Export_output"
Rem Shut down the SAS server
SASwsm.Workspaces.RemoveWorkspaceByUUID SASws.UniqueIdentifier
SASws.Close
Set SASws = Nothing
All the SAS codes seem to work through this except the ones that import csv files using proc import method. Can someone please explain me why
Thanks in advance
I am creating a portscanner (via Python) and whenever I receive something, it should be written in an .XLSV file.
What it should do:
Webscanner finds port 21 open
Receives data
Writes it in a .XLSV file in row 2
Webscanner finds port 80 open
Receives data
Writes it in a .XLSV file in row 3
My code:
wb = load_workbook('scanreport.xlsx')
hitdetails = (str(hostname), str(host), str(port), str(keyword), str(banner))
wb = Workbook()
ws = wb.active
start_row = 1
start_column = 1
for searchresult in hitdetails:
ws.cell(row=start_row, column=start_column).value = searchresult
start_column += 1
start_row += 1
wb.save("scanreport.xlsx")
Result:
How can I fix this?
#skjoshi, you sir just fixed my problem with overwriting:
write into excel file without overwriting old content with openpyxl (this page)
Because I am already loading an existing file with an existing sheet, I was also creating a new worksheet, which will overwrite the old one in which I lose my data everytime.
In this case I removed the ws = wb.active and it worked!
I am trying to create and serve a zip file for user that contains ical files for different workers (each worker has his own ical file)
The problem is that i get the right number of iCal files in my zip but the last file has all the data from previous workers in it. (also the one before etc.)
this is the code I am using
What am I doing wrong?
This is my code
cal = Calendar()
import zipfile, cStringIO
exported_chunks_zip = cStringIO.StringIO()
zipf = zipfile.ZipFile(exported_chunks_zip, "w", compression=zipfile.ZIP_DEFLATED )
for i, rec in enumerate(grouped):
worker = rec['rw_worker_nick'].encode('cp1250')
for rr in rec["allData"]:
startDate = rr['rw_date']
startTime = rr['rw_time_start']
endTime = rr['rw_time_end']
evtstart = datetime.datetime.combine(startDate,startTime)
evtend = datetime.datetime.combine(startDate,endTime)
event = Event()
event.add('summary', rec['rw_worker_nick'])
event.add('dtstart', evtstart)
event.add('dtend', evtend)
cal.add_component(event)
text = cal.to_ical()
zipf.writestr(worker +'.ics', text)
text = ''
any suggestions?
thank you
You create only a single Calendar object outside of the for loop and then keep appending events to it. You should instead create a new Calendar object for each worker within the for loop:
for i, rec in enumerate(grouped):
cal = Calendar()
...
I have a program that writes files to a network share at a high rate, from a few (3) threads at once.
After running for a while (usually a short while) some of these threads get stuck. Using Process Monitor, I can see that there are calls to WriteFile and CloseFile that simply have no answer.
At this point, I can't shut down the process at all, even killing it from the task manager does nothing.
The interesting thing is that this happens when the computer hosting the shares is running Windows Server 2008 (R2). If I move the shares to a Windows 2003 computer, I don't see these problems. Also, I only see this problem if the program is run on a computer that is running Windows Server 2008 (different computer than the share host).
Here is a short program that quickly reproduces the problem. The files in the source directory range in size from 1 to 20 MB:
Imports System.IO
Imports System.Threading
Module Module1
Private m_sourceFiles As FileInfo()
Private m_targetDir As String
Sub Main(ByVal args As String())
Dim sourceDir As New DirectoryInfo(args(0))
m_sourceFiles = sourceDir.GetFiles()
m_targetDir = args(1)
For i As Integer = 0 To 2
ThreadPool.QueueUserWorkItem(AddressOf DoWork)
Next
Console.ReadLine()
End Sub
Private Const BUFFER_SIZE As Integer = (128 * 1024)
Private Sub DoWork(ByVal o As Object)
Console.WriteLine(Thread.CurrentThread.ManagedThreadId)
Dim random As New Random(Thread.CurrentThread.ManagedThreadId)
While True
Dim fileIndex As Integer = random.Next(m_sourceFiles.Count)
Dim sourceFile As FileInfo = m_sourceFiles(fileIndex)
Dim input As FileStream = sourceFile.OpenRead
Dim targetName As String = sourceFile.Name.Replace(sourceFile.Extension, random.Next(Integer.MaxValue) & sourceFile.Extension)
Dim targetPath As String = m_targetDir & "\" & targetName
Dim output As FileStream = File.Create(targetPath)
Dim bytes() As Byte = New Byte((BUFFER_SIZE) - 1) {}
Dim read As Integer = input.Read(bytes, 0, bytes.Length)
While read <> 0
output.Write(bytes, 0, read)
read = input.Read(bytes, 0, bytes.Length)
End While
output.Flush()
output.Close()
Console.WriteLine(Thread.CurrentThread.ManagedThreadId & " - " & targetName)
End While
End Sub
End Module
The problem was caused by Symantec Antivirus.
Apparently they don't support 2008 R1 yet.
I was able to workaround the issue by disabling SMB 2.0 on the client computer, as described here:
sc config lanmanworkstation depend= bowser/mrxsmb10/nsi
sc config mrxsmb20 start= disabled