python error can't open pdf in application from python - python-2.7

I am using windows 10 and python 2.7.13 .
My target is to open a pdf in acrobat reader at a specific page number.
I am using the code I received from one of the questions in the forum here.
import subprocess
import os
path_to_pdf = os.path.abspath('C:\test_file.pdf')
# I am testing this on my Windows Install machine
path_to_acrobat = os.path.abspath('C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe')
# this will open your document on page 12
process = subprocess.Popen([path_to_acrobat, '/A', 'page=12', path_to_pdf], shell=False, stdout=subprocess.PIPE)
process.wait()
It opens up the acrobat reader application but the file doesnt open and i get the error :"There was an error opening this document. The filename, directory name, or volume label syntax is incorrect
but when I use these commands in cmd without python I am successfully able to open the pdf without any error. Please help.

Try to open the file with r:
path_to_pdf = os.path.abspath(r'C:\test_file.pdf')
path_to_acrobat = os.path.abspath(r'C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe')
Check this out too, it will show to you an example of an error with the white spaces on the path

Please check the path of your acrobat reader.
In my case:
path_to_acrobat = os.path.abspath('C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\AcroRd32.exe')

Related

xlwings: "Run main" cannot find filepath?

I'm new to xlwings - started with trying to understand basics but it is not working ...
Created .py and .xlsx files (with same name) in same folder; inserted basic "Hello World" code as main() in .py file.
Using Run main button in Excel file gives:
Run-time error '53': File not found
Tried to trouble-shoot why this could be: debugging via VBA interface found that the routine was searching in the folder C:\Users\MYNAME\AppData\LocalTemp\ (not the project's folder) and coming up with a very long .log filename which changes each time I attempt to run it (eg. xlwings-374ABEE7-4C51-8622-AB5B-D42C5294C2B8.log)
Is this a bug which needs to be corrected? or have I done something incorrectly?
Using:
Windows 10;
MS Office 365;
xlwings ver: 0.19.5

rmarkdown error: "myfile.tex"' had status 1

I am trying to compile an RMarkdown document from an R script. This is the code I am running:
setwd("C:/Users/me/me_VN02_5676/myfolder")
rmarkdown::render("myrmd.Rmd",
output_file = "mypdf.pdf")
This results in the error:
Error: Failed to compile mypdf.tex.
In addition: Warning message:
running command '"pdflatex" -halt-on-error -interaction=batchmode "mypdf.tex"' had status 1
Does anyone know what is happening?
Additional Notes
The PDF file is created when I use the Knit button in RStudio
This code does produce the .Tex file and when I run that it produces the pdf but render() does not produce the pdf
When I run the .R file via the command line it works! The PDF is created but why doesn't it work in R studio?
It would help to provide a Minimal Working Example so that we can attempt to replicate the problem. However, there may be two potential causes:
File Paths Look Incorrect
In R, they should be specified with a single forward slash: https://stat.ethz.ch/R-manual/R-devel/library/base/html/file.path.html
setwd("C:/Users/me/me_VN02_5676/myfolder")
rmarkdown::render("myrmd.Rmd",
output_file = paste0("mypdf.pdf")
)
PDF Output File May be Open
Another potential cause of this is that the PDF file is open, and therefore pandoc cannot save the output PDF. Try closing the PDF and render the document again.
Link to previous issue in Knitr which relates to this: https://github.com/yihui/knitr/issues/1209

python write-to-file issue, cannot understand behavior

Env: Python 2.7
I write some code that write list terms into text files.
Windows sublime remote connected to ubuntu server to edit files
Behavior:
I use file cmd to see the property of the text file, find something interesting.
I guess it could be "xx.txt UTF-8 Unicode text",
but actually it is "xx.txt: DOS executable (COM)"
By mistake I use with clause and f.close() method at the same time. I cannot understand behavior that happens.
Code:
with open(output, 'w') as f:
for comment in sample_list:
f.write(comment + '\n')
f.close()

subprocess.Popen unexpected behavior

I inherited some Python code which uses subprocess.Popen to print a pdf file using Adobe Reader. The code has worked well for years running on Windows 7, I am getting it set up on Windows 10. For some reason the call to Popen starts an Adobe Reader process, but does not open the application. The code works as expected if I run it from the Python interpreter. But when it when it is running as a Windows process, Adobe Reader does not open, or print the pdf. I checked the command line parameters for the running process and it is invoked exactly the same as when run from the Python Interpreter. A code snippet is below.
printFileName = r'"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe" /p /h /t '
printFileName = printFileName + filename
subprocess.Popen(printFileName)
filename is the full path to a .pdf file.
I'm not really sure where to even begin figuring this out so any pointers or suggestions on how to improve the question would be appreciated.

command prompt appears than immediately disappears

I encountered this problem after following a python tutorial on Youtube about creating text files. The instructor had us type in the following code to start:
def createFile(dest):
print dest
if__name__ == '__main__':
createFile('ham')
raw_input('done!')
We had created a folder on the desktop to store the file 'ham' in. But when I double clicked on 'ham' the command prompt window popped on then in a flash it popped off. I am an obvious beginner and I don't know what is going on. Can anyone explain this to me?
You can open command prompt then navigate to python interpreter
directory and run your program by typing python /diretory/to/your/program.py for
example if you have a program named test.py in the directory c:/python and you want to
run it and you have python interpreter installed in C:/python2.x/ directory
then you should open command prompt and type in
cd c:\python2.x\
then you should type
python c:/python/test.py
and perfectly it will work
showing you your program output .