Let me start off by saying my python knowledge is beginner-to-intermediate level, and I recently started using the language again after a long time.
The Goal:
This morning I came across a bunch of word documents I wanted to convert and concatenate to PDF files, with 2 .doc files creating one PDF.
seemed like a fairly trivial task, so I figured I'd try to learn how to do it in python.
concatenating PDFs wasn't too bad, I found PyPDF2 and managed to write a script that did just that.
But 7 hours later, after countless scripts with broken dependencies- I still can't find a way to automate the doc-pdf conversion.
The Problem(s):
every script I found either:
uses python-docx (my documents are word 2003 .docs)
uses unoconv bridge (which I installed along with OpenOffice, then searched around for documentation but found none- thus I have no idea how to call from a python script or the shell. I saw one example for this but it keeps throwing errors)
uses win32com or win32com.client or pywin32 or somesuch.
I ran into numerous issues with these- installed one but couldn't import it from code (as happened to the guy here), now I can't even find them with pip. searched for documentation for them (are they modules or classes? I have no idea) and found practically nothing that I could understand, beyond that they're connected to ActivePython. (which is apparantly a superset of Python with more capabilities?).
Uses comtypes, which I installed but was unable to use/import either for some reason (maybe I'm using pip wrong somehow?)
I know my question is hardly focused but honestly by now my brain is fried from information overload. any simplifications for a noob would be more than welcome.
TL;DR:
assuming no knowledge of COM stuff and little experience with any external frameworks:
what would I have to do to convert Word 2003 .doc files to .pdf files? I'm running python3.5.1 32-bit on a Windows 10 64-bit machine.
where can I learn more about accessing other software APIs from python? are there big prerequisites for this stuff like knowing how the OS works on a lower level?
Thanks!
From my experience, converting between the various office formats is best done outside of python. With the subprocess module, you can call the external command
soffice --convert-to pdf file.doc --headless
where soffice is the command that comes with LibreOffice.
Related
So I am creating a program that takes input, processes the data, then puts it in Excel. In order to do this, I am using the "xlwt" package (and possibly xlrd). How do I then give this program to other people without making them download python and the packages associated with my program? I considered utilizing an online python interpreter and giving the username/password to my coworkers, but xlwt isn't on any of the ones I've tried, and they don't offer a way (that I can see) to download new packages.
You would have to compile the code into an exe file. The py2exe library can help you out with this
I know this is possible to do using additional libraries such as win32com or python-pptx, but I wasn wondering if anyone knew of a way to insert an image into a powerpoint slide using the standard libraries. Lots of googling has indicated that the best solution is probably win32com, but since I can guarantee that every system this script will be deployed to will have win32com, I am looking for an implemention leveraging libraries all systems with a standard python 2.7 install will have.
It is probably possible to modify a .pptx file with the standard library without much effort: these new generation of files are meant to be zip-compressed XML + external images files, and can be handled by ziplib and standard xml parsers.
Legacy .ppt files however are a binary closed format, with little documentation, and hundrededs of corner cases. It would alwasys "be possible" to change them, since they are still just bytes, but it would take considerable effort.
That said, starting with Python 3.4, the Python installer "PIP" comes default with the language install: probably the best way to go would be to script the installation of external libraries based on the built-in PIP - that way one would not have to all external library usage.
I have written a program. I don't know if it is important how it is written but you can find it here: http://pastebin.com/Z3ZvVPV8 Basically, it asks you to assign values to variables and will perform calculations depending on what variables you chose, and prints the answer.
I would like to know how I can make the program run in a window other than cmd (I am using Windows Vista 32bit). I don't need much at all in terms of GUI, just a window that is a bit more user friendly/easier to look at when they are using the program.
EDIT: To those suggesting using IDLE, while that would work for me, if others want to use the program they would have to download it, so I was hoping for a way for that not to happen.
Python comes with a sort of default GUI package TkInter you can use it.
Also there is a lot of other GUI packages available.
The Python standard library offers a lot of ways to implemt simple (but also rather complex) GUIs. I'd like to point you at the documentation of TK (tool kit for graphical interfaces) http://docs.python.org/library/tk.html where you will find also some useful example of use.
Py2Exe is a viable option if you really don't need a gui. This will make it run and look like a command prompt, but it will be an .exe file. Here is a quick quote from thier page: "py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation."
Another alternative is to get Portable Python. Here is a quote from thier webpage: "Portable Python is a Python® programming language preconfigured to run directly from any USB storage device, enabling you to have, at any time, a portable programming environment. Just download it, extract to your portable storage device or hard drive and in 10 minutes you are ready to create your next Python® application." After packaging the portable python and your .py or .pyc file then create a .bat file that runs the portable python "Python-Portable.exe" with the correct command line parameters for loading your script. Be sure to use relative paths in the batch file in case they are running it from a flash drive, or something other than the same location as you.
NOTE: This is really not a good way to do this as thier download page states: "Installed size: based on selected packages, between 49MB and 480MB". Also be sure to read the the current Python Software Foundation License, as that is what Portable Python is released under, and it may or may not be legal to package it in a closed source project. I haven't really looked at the license myself to be able to tell you. If you are releasing it as open source, then there would not be an issue though. As a quick side note, if you need that .bat file to be a .exe file then you can use a .bat to .exe converter battoexe.com is one. This is really going the long way about doing the whole thing, but it is an option.
Sources:
Working with Python on and off for 7 years now, a lot that using a portable version on a flash drive, and also dealing with Batch files much longer.
For a website i am developing in django i need users to be able to upload .wav or .aif files. I, of course, have to make sure these files really are what they pretend to be - audiofiles. The files then are provided on the webpage, where i need them to be either .ogg or .mp3
While searching for a solution i stumbled across some fearsome possibilities, like using ctypes to handle external libraries. I also found, of course, PyMedia, which i cannot use because i develop on MacOSX. And the python audio tools provide a lot of functionality i do not need.
So far i can see a few possibilities that would satisfy me and are within reach of my programming capabilities:
1 Get PyMedia to run on MacOSX
2 Find a way to use some modules of the python audio tools without the need to use libcdio
3 use python subprocess to run the command line tools of the converters
As i have used none of those tools yet, i can't tell which would possibly be the quickest way to solve my problem. If you Python-Audio-Gurus are out there, could you please share some thoughts? Or maybe you even have a fantastic 1-step-to-happiness solution?
Not strictly a pythonic answer, but perhaps take a look at sox which is a simple command line audio file converter. It can do resampling of audio files for you as well.
Check out the command line options of sox for details. This will of course involve calling the external program using the subprocess module(or other method).
I'm trying to write my own media player (like Foobar), and I'm having trouble tracking down a Python library that'll play MP3s. I know Pymedia does mp3s, but it looks outdated - the latest installer is for Python version 2.4, and I'm using 2.6. I've never had much success with Pygame, and Pyglet doesn't look like it has too much in the way of documentation. Are there any other alternatives?
There is http://pyglet.org/ and also have you tried http://code.google.com/p/mp3play/? It's also available from PyPi (http://pypi.python.org/pypi/mp3play/) However, I think mp3play is Win32 only for now.
Looking at the updates, there were commits within last couple of months.
I've been using PyMedia in Python 2.6.5 on Windows successfully. Caveats: the documentation is bad and wrong -- many of the tutorials have glaring errors or otherwise don't work -- so I had to do some experimentation and Googling to get my code to work right. Also for whatever reason the maintainers seem to have stopped updating the project site 4 years ago, though they seem to be actively doing something.
I found installers here:
http://www.lfd.uci.edu/~gohlke/pythonlibs/
The semi-active forum linked from their website includes some code maintainers who are semi-helpful. I'm jboyd99 if anyone is looking for tips.
For reasons that are beyond me the focus is on car audio systems, despite the fact that it is a fairly fully featured library that does some things no other free Python library does, like read MP3s into raw PCM data. The library has some flaws -- I'll probably use PyAudio or PyAudiere for actual playback for better control of synchrony issues.
Maybe it'd be simpler to write that part of your application in Python 2.4 as a separate "backend". This way you could use PyMedia (http://pymedia.org/) (as you mentioned) for the actual playback. It'd allow you to write your GUI in another Python version (like 2.6), which would also mean more decoupling of program components and parallelism (smoother GUI).
If you target only the Windows platform, then using Media Player via COM might help:
http://www.daniweb.com/code/snippet216465.html