How rename multiple files adding one symbol at the end of files
From this:
New Text Document.txt
New Text Document (2).txt
New Text Document (3).txt
New Text Document (4).txt
To this:
New Text Document1.txt
New Text Document2.txt
New Text Document3.txt
New Text Document4.txt
Thanks!
With an input of
from os.path import splitext
file_list = ["New Text Document.txt", "New Text Document (2).txt",
"New Text Document (3).txt", "New Text Document (4).txt"]
You get the desired output with
def parse(file, no):
body, ext = splitext(file)
new_body_str = ' '.join(body.split()[:3])
return f"{new_body_str}{no}{ext}"
output_list = [parse(filename, no+1) for no, filename in enumerate(file_list)]
If you need this to work for other files than New Text Document use a regular expression instead of body.split()[:3]
Related
I'd like to insert the text string into the existing rich text field data at the first position for all of documents in a DB.
NotesRichTextNavigator.FindFirstElement method - This method needs to specify the element type to search but I simply insert the text at the first position of the rich text data.
This might be very basic question, but I could not find the way and waste a few hours... Please help me!
You can do this using a workaround. Instead of working with FindFirstElement, you create a dummy richtextitem, containing the text that you need to prepend to your original item,
add the original item to the dummy item, delete the original item and recreate it.
Then add the dummy item and delete the dummy.
This sounds complex, but it is not that hard actually. Here's a small example in LotusScript on how to do this on a document:
'Get your richtext field
Set rtf = doc.getfirstItem("myRTF")
'create the dummy
Set rtDummy = doc.Createrichtextitem("rtfDummy")
'set the text that you want to insert in your richtext field
Call rtDummy.appendText("Inserting a line of text at the top")
'Add a line to make sure the inserted text is on a separate paragraph
Call rtDummy.Addnewline(1, true)
'Add the content of the original richtext item
Call rtDummy.Appendrtitem(rtf)
'Remove the original item and recreate it
Call rtf.Remove()
Set rtf = doc.Createrichtextitem("myRTF")
'Append the dummy item (including the added text)
Call rtf.Appendrtitem(rtDummy)
'Remove the dummy item
Call rtDummy.Remove()
'Save the document
Call doc.Save(True, True)
I am working on an existing word report and trying to do some automation with python docx module. I need to get the report date from database and paste it to "cover page" of the doc but I couldn't find any attribute about cover page in module. How can I do it?
What you can do is:
In the word document write any text where you want to replace the date picked from your database eg: dd-mm-yyyy
You can now search for your entered text "dd-mm-yy" in the word file using regular expressions and replace it with the Date you got from your database.
The code will be as follows:
def docx_replace_regex(doc_obj,replaceDate):
regex = re.compile(r"dd-mm-yyyy")
for p in doc_obj.paragraphs:
if regex.search(p.text):
p.text = regex.sub(replaceDate, p.text)
doc.save('generatedDocument.docx')
filename = "Your Word Document Path.docx"
doc = Document(filename)
docx_replace_regex(doc,date)
I would like to open a new file and then replace this empty file with the strings from my list. Each list item on a separate line. I am using Sublime 3. Currently my plugin opens the new file, but does not change the view to the new file to edit (add the strings from my list).
I have the following code:
size = len(TheList)
count = 0
view = self.view.window().new_file()
allcontent = sublime.Region(0, self.view.size())
while size!=count:
self.view.replace(edit, allcontent, TheList)
count+=1
I need to use:
self.view = self.view.window().new_file()
I am trying to get files from filedialog and display the name of the file names inside the listview. And also checkboxes should also be created before the filenames inside the listview based on the number of files added. Below is my code that returns only one file with the check box, irrespective of any number of files selected. Help would be appreciated.
def OpenTheFile(self):
file = QtGui.QFileDialog.getOpenFileNames(self.dlg, "Select one or more files to open", os.getenv("HOME"),'.sql (*.sql)')
str_file = ','.join(file)
fileinfo = QFileInfo(str_file)
filename = QFileInfo.fileName(fileinfo)
if fileName:
for files in str_file:
model = QStandardItemModel()
item = QStandardItem('%s' % fileName)
item.setCheckable(True)
model.appendRow(item)
self.dlg.DatacheckerlistView2.setModel(model)
self.dlg.DatacheckerlistView2.show()
It is really unclear what you are doing (there is very little context to your question), but the following code should work. There were several issues with the code in your original question, namely:
You were joining all of the files into a single string and iterating over the string, rather than iterating over the list of filenames.
You were recreating the model each iteration of your loop, effectively deleting any previously added rows.
The code:
files = QtGui.QFileDialog.getOpenFileNames(self.dlg, "Select one or more files to open", os.getenv("HOME"),'.sql (*.sql)')
if files:
model = QStandardItemModel()
for file in files:
item = QStandardItem('%s' % file)
item.setCheckable(True)
model.appendRow(item)
self.dlg.DatacheckerlistView2.setModel(model)
self.dlg.DatacheckerlistView2.show()
I have a Worddocument with bookmarks. From Excel I write cell content to the places where I set the bookmarks.
My problem: You can still see the bookmarks.
What I tried:
First I used a placehoder bookmark with
.item("Name1").Range.InsertAfter Rep.NName1
Second I used an enclosing bookmark with
.item("Name1").Range.InsertAfter Rep.NName1
and
.item("Name1").Range.InsertBefore Rep.NName1
I still cannot get rid of the bookmarks.
All I could do is using the sledgehammer approach and delete them but I think there should be a way to replace them during the insert.
Source
If you want to overwrite the bookmark (ie replace any text contained within the bookmark and delete the bookmark itself), you can just set the Text property of the Bookmark's range:
.Item("Bookmark1").Range.Text = "Some new text"
If you want to replace the content of the existing bookmark but identify the new text with the bookmark, you'll need to replace the text then mark the new text as the bookmark:
Dim bmRange As Range
Set bmRange = .Item("Bookmark2").Range
bmRange.Text = "Some new text"
.Add Name:="Bookmark2", Range:=bmRange