import full .txt with SET !DATASOURCE not only certain line on imacros - imacros

I am trying to use the command
SET !DATASOURCE
to import the whole text from ".txt" file into some textbox
but I cannot find the exact command that would let me import/copy
the whole textfile into the box text, so far I have found {{!COL1}} in SET !DATASOURCE instruction page but It only imports the first line from the .txt file
and not full text
is there any way that I could import/copy whole text file?
and not only some certain lines!
code example:
SET !DATASOURCE TOPIC.txt
SET !DATASOURCE C:\TOPIC.txt
TAB T=1
TAB CLOSEALLOTHERS
'SET !PLAYBACKDELAY 0.00
URL GOTO=https://www.********.com
FRAME F=1
EVENTS TYPE=KEYPRESS SELECTOR="HTML>BODY" CHARS="{{!COL1}}"
FRAME F=0
Using Imacros software - Windows 64

Hum, "using iMacros (correct Spelling btw) Software" is a bit vague, if you don't mention the Version...! (And "Windows 64" also...)
=> FCI = iMB v2021.0 'Trial' + Win10_x64. (From your parallel Thread...)
But OK, same Answer/Solution like in your parallel Thread on the iMacros Forum:
Re: import full .txt with SET !DATASOURCE not only certain line
Yep..., '.TXT' Extension is perfect, (doesn't work with '.CSV' Extension), open your File in a TAB_2, (with URL GOTO + file Protocol), and extract it (=> EXTRACT=TXT) on TYPE=PRE, that will extract the whole Content of the File...
(BODY or HTML for the TYPE Param work also, but PRE is the "cleanest"...)
Drag your File from 'Windows Explorer' once to some open Tab in iMB (or any Browser), or open it through 'File > Open File' to get the full URL to reuse for URL GOTO...
That works with iMacros for FF v8.x (v8.8.2 + v8.9.7), I would think it (probably?) also works for iMB v2021.x, if it supports the file Protocol, which I would expect is the case... (I cannot try/test, I installed some 'Trial' Version a few months ago, is already long finished... )
... And just to be "clear", URL GOTO + EXTRACT do "the Job", you don't need !DATASOURCE + !COLn...

Related

select some of (EOF) extension files in specific folder based on part of filenames in text file :explained in image file with connected arrow

I want to Select files in specific folder based on part of the name of files(date)
so:
1-first: (in names.csv) extract part of file name(date) :find all("1SDV_(\d+)T", name)
2-second: (in name of files in specific folder) extract part of file name(date):
find all ("OPOD__(\d+)T", name)
of all (EOF) extension file names in specific folder
3-third: relate extraction: as image that I attached (red connected arrow)
my code is not complete please help me to complete it
enter code here
import OS
import re
# open the file, make a list of all filenames, close the file
with open('names.csv') as name file:
# use .strip() to remove trailing whitespace and line breaks
names= [line strip() for line in name file]
for name in names:
res = re find all ("1SDV_(\d+)T", name)
if not res: continue
print res[0]
# You can append the result to a list
# Find all ("OPOD__(\d+)T")
# how can I relate (EOF) extension files name to res like image file I
attached
# move the selected (EOF) extension file
OS rename(OS path join('path', (EOF) file extension folder),
'/path/to/somewhere/else')
break
csv or text file is like this:
S1A_IW_SLC__1SDV_20190826T022837_20190826T022904_028734_0340DD_654D-SLC
S1A_IW_SLC__1SDV_20190919T022838_20190919T022905_029084_034D09_0129-SLC
S1A_IW_SLC__1SDV_20191013T022839_20191013T022906_029434_03590E_A824-SLC
S1A_IW_SLC__1SDV_20191106T022839_20191106T022906_029784_036538_06CC-SLC
S1A_IW_SLC__1SDV_20191130T022838_20191130T022905_030134_037166_4019-SLC
S1A_IW_SLC__1SDV_20191224T022837_20191224T022904_030484_037D7B_0FC4-SLC
S1A_IW_SLC__1SDV_20210217T062720_20210217T062747_036626_044D90_4570-SLC
.
.
.
And (EOF) extension files in specific folder are like :
S1A_OPER_AUX_POEORB_OPOD_20190915T120743_V20190825T225942_20190827T005942.EOF
S1A_OPER_AUX_POEORB_OPOD_20190916T120658_V20190826T225942_20190828T005942.EOF
S1A_OPER_AUX_POEORB_OPOD_20190917T120653_V20190827T225942_20190829T005942.EOF

Writing in files

I have been trying to write onto a file while using python but for some reason it keeps writing onto my console and not my created file. Yes I know this question has been asked before and yes i have used the .close() command. Here is my block of code.
myfile= open ('C:/Users/12345/Documents/Grouped_data.txt','r')
with open ('C:/Users/12345/nanostring.txt','w') as output:
for line in myfile:
Templist= line.split()
print line
print Templist[0], Templist[4], Templist[5],Templist[6], Templist[7], Templist[8], Templist[9], Templist[10], Templist[12]
print output
myfile.close()
output.close()
This should be as simple as:
>>> with open('somefile.txt', 'a') as the_file:
... the_file.write('Hello\n')
From The Documentation:
Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single '\n' instead, on all platforms.
In python 2.7,
You can use >> after the print and use as name
So here it is print>>output,line
myfile= open ('C:/Users/12345/Documents/Grouped_data.txt','r')
with open ('C:/Users/12345/nanostring.txt','w') as output:
for line in myfile:
Templist= line.split()
print>>output,line # Note the changes
print>>output,Templist[0], Templist[4], Templist[5],Templist[6], Templist[7], Templist[8], Templist[9], Templist[10], Templist[12] # Note the changes
Note: print directly prints in terminal and print>>as name, prints to file.

LPTHW Ex 16, imy ssues with my own script

I successfully completed ex16 in LPTHW and now I'm trying to replicate it in my own script to better understand the lesson. I typed the following but the shell returns with:
File "bruce.py", line 23, in
scribble.truncate()
I0Error: File not open for writing
My script is as follows:
from sys import argv
script, file_name=argv
scribble=open(file_name)
print "Master Bruce, here is your file: %s" % file_name
print scribble.read()
print """
Master Bruce, to change the contents of the file
simply press ENTER and type three lines:
"""
line1=raw_input("line 1:")
line2=raw_input("line 2:")
line3=raw_input("line 3:")
print "Just a few seconds Master Bruce..."
scribble.truncate()
scribble.write(line1,line2,line3)
scribble.close
My understanding is that the file was opened in line 5 already. I also tried scibble.open() on line 22 but that didnt work either. Your help is appreciated.
It means exactly what it says: the file isn't open for writing. You opened it in read-only mode.
scribble=open(file_name)
is equivalent to
scribble=open(file_name, "r")
You need to open the file in read/write mode. Since you don't want to truncate it at the start and don't want to append to it, use r+.
scribble=open(file_name, "r+")
You should brush up on the documentation for open() here.
Incidentally, you should also look into opening files with the with keyword here for cleaner handling.
with open(file_name, "r+") as scribble:
# do things
...
The most commonly-used values of mode are 'r' for reading [...]. If mode is omitted, it defaults to 'r'.
[...]
Modes 'r+', 'w+' and 'a+' open the file for updating (reading and writing); note that 'w+' truncates the file.
source

Giving multiple file names as link to Tkinter GUI in python and opening with default application

#python version 2.7.11
import os
from Tkinter import *
fileName = "Nothing" #fileName is required so that the callback function can link the current file name with button click
def callback(event):
#os.startfile() works only on windows machine
os.startfile(fileName) #opens the fileName with its default application (in my case a pdf viewer)
def makeLink(file):
global fileName
fileName = file
link = Label(textbox, text=fileName, fg="blue", cursor="hand2")
link.pack()
link.bind("<Button-1>", callback)
oldDir = os.getcwd()
newDir = os.chdir("pdf folder") #pdf folder contains some pdf files
root = Tk()
textbox = Text(root)
textbox.pack()
allFiles = os.listdir(os.getcwd()) #makes a list of files present in current directory
for file in allFiles:
makeLink(file)
root.mainloop()
os.chdir(oldDir)
I want to select the files of a folder (in above code it's pdf folder) containing some pdf files and display them as a link in GUI window. When pressed on the link, the file should open with it's default application (so the pdf file should open with default pdf viewer). My code is displaying the file names of every file in the "pdf folder" as link. But on clicking on every link, the last file of the folder is opening. Can someone correct the code so that every link opens it's own file. I searched everywhere but found nothing on this topic.
This is exactly what you have coded so far. You're making fileName global and then setting fileName = file where the last assignment will be the last item in your loop. So, this is why it is opening the last file. If you want a user to be able to select options from these I'd suggest a combobox or some other widget that takes the list of filenames as the options. You can then bind on <<ComboboxSelected>> to call your makeLink function.
you should really also rename file. file is a built in.
This problem can surely be done by combobox but when i have many pdf files then every file is placed in a combobox which is not a good way to interact.
Here I have taken two count variables ---- one is global count and one localCount. The localCount is required to remember the count of which file is to be opened.
from Tkinter import *
import os
def callback(count):
os.system("open "+fileNames[count])
def makeLink(files):
localCount = count
link = Button(frame1, text=(count , files), bg="light blue", cursor="hand2", command=lambda: callback(localCount))
link.pack()
oldDir = os.getcwd()
newDir = os.chdir("pdf folder")
fileNames = os.listdir(os.getcwd())
root = Tk()
frame1 = Frame(root)
frame1.pack()
count = 0
for files in fileNames:
makeLink(files)
count += 1
root.mainloop()
os.chdir(oldDir)

How do you shift all pages of a PDF document right by one inch?

I want to shift all the pages of an existing pdf document right one inch so they can be three hole punched without hitting the content. The pdf documents will be already generated so changing the way they are generated is not possible.
It appears iText can do this from a previous question.
What is an equivalent library (or way do this) for C++ or Python?
If it is platform dependent I need one that would work on Linux.
Update: Figured I would post a little script I wrote to do this in case anyone else finds this page and needs it.
Working code thanks to Scott Anderson's suggestion:
rightshift.py
#!/usr/bin/python2
import sys
import os
from pyPdf import PdfFileReader, PdfFileWriter
#not sure what default user space units are.
# just guessed until current document i was looking at worked
uToShift = 50;
if (len(sys.argv) < 3):
print "Usage rightshift [in_file] [out_file]"
sys.exit()
if not os.path.exists(sys.argv[1]):
print "%s does not exist." % sys.argv[1]
sys.exit()
pdfInput = PdfFileReader(file( sys.argv[1], "rb"))
pdfOutput = PdfFileWriter()
pages=pdfInput.getNumPages()
for i in range(0,pages):
p = pdfInput.getPage(i)
for box in (p.mediaBox, p.cropBox, p.bleedBox, p.trimBox, p.artBox):
box.lowerLeft = (box.getLowerLeft_x() - uToShift, box.getLowerLeft_y())
box.upperRight = (box.getUpperRight_x() - uToShift, box.getUpperRight_y())
pdfOutput.addPage( p )
outputStream = file(sys.argv[2], "wb")
pdfOutput.write(outputStream)
outputStream.close()
You can try the pypdf library. In 2022 PyPDF2 was merged back into pypdf.
two ways to perform this task in Linux
using ghostscript trough gsview
look in your /root or /home for the hidden file .gsview.ini
go to section:
[pdfwrite Options]
Options=
Xoffset=0
Yoffset=0
change the values for X axis, settling a convenient value (values are in postscript points, 1 inch = 72 postscript points)
so:
[pdfwrite Options]
Options=
Xoffset=72
Yoffset=0
close .gsview.ini
open your pdf file with gsview
file / convert / pdfwrite
select first odd pages and print to a new file (you can name this as odd.pdf)
now repeat same steps for even pages
open your pdf file with gsview
[pdfwrite Options]
Options=
Xoffset=-72
Yoffset=0
file / convert / pdfwrite
select first even pages and print to a new file (you can name this as even.pdf)
now you need to mix these two pdf with odd and even pages
you can use:
Pdf Transformer
http://sourceforge.net/projects/pdf-transformer/
java -jar ./pdf-transformer-0.4.0.jar <INPUT_FILE_NAME1> <INPUT_FILE_NAME2> <OUTPUT_FILE_NAME> merge -j
2: : use podofobox + pdftk
first step: with pdftk separate whole pdf document in two pdf files with only odd and even pages
pdftk file.pdf cat 1-endodd output odd.pdf && pdftk file.pdf cat 1-endeven output even.pdf
now with podofobox, included into podofo utils
http://podofo.sourceforge.net/about.html
podofobox file.pdf odd.pdf crop -3600 0 widht height for odd pages and
podofobox file.pdf even.pdf crop 3600 0 widht height for even pages
width and height are in postscript point x 100 and can be found with pdfinfo
e.g. if your pdf file has pagesize 482x680, then you enter
./podofobox file.pdf odd.pdf crop -3600 0 48200 68000
./podofobox file.pdf even.pdf crop 3600 0 48200 68000
then you can mix together odd and even in a unique file with already cited
Pdf Transformer
http://sourceforge.net/projects/pdf-transformer/
With pdfjam, the command to translate all pages 1 inch to the right is
pdfjam --offset '1in 0in' doc.pdf
The transformed document is saved to doc-pdfjam.pdf. For further options, type pdfjam --help. Currently pdfjam requires a Unix-like command prompt (Linux, Mac, or Cygwin). In Ubuntu, it can be installed with
sudo apt install pdfjam
Not a full answer, but you can use LaTeX with pdfpages:
http://www.ctan.org/tex-archive/macros/latex/contrib/pdfpages/
Multiple commandline linux tools also use this approach, for instance pdfjam uses this:
http://www2.warwick.ac.uk/fac/sci/statistics/staff/academic-research/firth/software/pdfjam
Maybe pdfjam can already provide what you need already.
Here is a modified version for python3.x.
First install pypdf2 via pip install pypdf2
import sys
import os
from PyPDF2 import PdfFileReader, PdfFileWriter
uToShift = 40; # amount to shift contents by. +ve shifts right
if (len(sys.argv) < 3):
print ("Usage rightshift [in_file] [out_file]")
sys.exit()
if not os.path.exists(sys.argv[1]):
print ("%s does not exist." % sys.argv[1])
sys.exit()
path=os.path.dirname(os.path.realpath(__file__))
with open(("%s\\%s" % (path, sys.argv[1])), "rb") as pdfin:
with open(("%s\\%s" % (path, sys.argv[2])), "wb") as pdfout:
pdfInput = PdfFileReader(pdfin)
pdfOutput = PdfFileWriter()
pages=pdfInput.getNumPages()
for i in range(0,pages):
p = pdfInput.getPage(i)
for box in (p.mediaBox, p.cropBox, p.bleedBox, p.trimBox, p.artBox):
box.lowerLeft = (box.getLowerLeft_x() - uToShift, box.getLowerLeft_y())
box.upperRight = (box.getUpperRight_x() - uToShift, box.getUpperRight_y())
pdfOutput.addPage( p )
pdfOutput.write(pdfout)