I'm developing an application in PyQt4 that eventually has to open and show PDF files. For this task there is a python library: python-poppler (in various spelling flavours).
The problem is that it is terribly under documented and the only simple working example I found so far uses Python+Gtk+Cairo, while the example with Python+Qt I found uses an older version of the library, and many major changes have occurred ever since, hence it doesn't work anymore.
It's a week I'm trying to use the code in the PyGtk example to hack the code of the PyQt one, but no success so far.
Has anybody got a simple example of a Python-Qt program that opens and shows a PDF file, which might be useful to the community to see how to work with that library?
Thanks a lot.
Archive with broken pyqt example
Archive with working PyGtk example
There is an example buried deep within an experimental (unused) branch of an app, here is a link to the specific file containing the code. Don't know if it'll help? All the relevant poppler code is self contained within the PdfViewer class at the bottom of that file.
http://bazaar.launchpad.net/~j-corwin/openlp/pdf/annotate/head:/openlp/plugins/presentations/lib/pdfcontroller.py
Related
I followed a tutorial for websockets in python and stumbled across the issue that pylance does not reccomend me the fuctions related to the class that I have imported from a module:
My Editor:
The Tutorial:
The Code Itself runs without any issue, so the Import seems to work, but I dont recieve the reccomendations in vs code. What is the reason for this or where could i debug something like this?
Thanks to wjandrea I found some thing I personally did not stumble acros before.
So a classic mistake happened to me when following a tutorial, I work on newer versions than the 1.5 year old video... Unfortunately the part talking about versions was in a nother part...
Long story short, in the mean time a bigger change for the websockets module appeared since the used functions are now imported lazily. Wich makes sense to reduce startup time incase you'll run a websocket server with the module.
Little info about lazy imports (for me it was the first time i heard about this intelligent feature)
Incase anybody else stumbles across this im currently on Python 3.10.7 and I am talking about websockets 10.3!
Back to the issue.
Pylance obviously can't make any reccomendations since the functions like websockets.connect(uri, ...) are just loaded if they are used in the runtime by the default websockets module so tools for code reccomendations inside the editor dont not know they are there..
I took a glance inside the module and through the indirect hint from wjandrea about the lazy imports inside __init__.py the listed dictionary made much more sense now! Based on this I could backtrack the Python scripts I need for my functions or rather Pylance needs to create those handy reccomendations for me inside vs code (or any other ide).
For now I just manually imported the desired script so I have a bit more guidance while writing and since startuptime in my current project is not crutial I let the manual imports exist or i´ll just change out the import variations based on the cirumstance if I am currently developing or if the code goes into production.
I made my own game in python using turtle graphics and I want to convert it to an executable file to run in HTML, is there any additional information I should know
Edit: An executable is not likely what you're looking for per Chris' answer. He also provides info about making an installer, if you do want to accomplish that. My answer addresses getting your Python game running in the browser.
When I initially saw this question, I thought it wasn't a thing. However, Python is just a language. Anything can interpret and run Python logic. It's just a matter if someone has built something that works in the browser and interprets Python.
After some Google searching, I found Skulpt: http://www.skulpt.org/. It appears to be a Javascript implementation of a Python interpreter. This is important because browsers only understand Javascript, not Python.
Something worth noting is that Skulpt appears to only have partial support for Python 3. It mostly focuses on Python 2. This could be a major concern because Python 2 is scheduled for end of life January 1st, 2020 (at least, that's what pip told me).
There are examples of using Skulpt on its page. Heads up that you'll likely need to learn some amount of Javascript, but if you're comfortable with Python already, that shouldn't be terrible.
Edit:
There also appears to be Brython: https://brython.info/. This is a Python 3-focused implementation. At first glance, it looks a little easier to use, too.
Hope this helps!
Please know that executables (.exe) & browsers tend not to work together.
See this stackoverflow question referring to .exe's & html The long & short of it is that you can setup .exe's so they can be downloaded, but not rendered in the browser.
If you want to convert your script to a .exe file, you'll need something like pyinstaller. Here's a link to some info about installing & using it.
TheBeege's answer is on point with rendering in the browser with python when you already have code written with turtle. I just wanted to make sure anyone coming here looking for info on python with executables could find it.
I'm new to Python and trying to get comfortable with the syntax and the language. I gave PyCharm a shot and found it very comfortable.
The only problem is that auto-completion isn't working as I expected and it is very important to me as part of the learning process and looking into some modules.
My code works even without the autocomplete but I'm very used to it and really wish to enjoy this feature.
I tried changing my project interpreter back and forth and nothing changed. I tried restarting PyCharm, the computer - didn't work. I tried Invalidate Cache, made sure the power save mode is off - nada.
Here is an example of missing autocomplete for lxml:
And here is the interpreter window:
Python is a dynamically typed language, so the return type of a function is not always known in advance. PyCharm generally looks at the source of a function to guess what it returns. It can't in this case because etree.parse is written in Cython, not Python. If you ask PyCharm to go to the definition of the function it gives you a stub.
The Python ecosystem has recently started to tackle this problem by providing various ways to annotate files with type hints for use by external tools, including PyCharm. One way is through .pyi files. A large collection of these can be found in the typeshed project. This issue shows that writing hints for lxml was proving difficult, and not wanting to have incomplete stubs in the typeshed repo, they were moved to their own repo here. The stubs are indeed very incomplete, and when I tried downloading and using them in PyCharm the results were pretty dismal. They correctly identify that etree.parse returns an etree._ElementTree, but the stub for _ElementTree only has two methods.
I got much better results by annotating directly in the Python file, e.g.
tree = etree.parse(path) # type: etree._ElementTree
(you can find out the type by checking type(tree))
PyCharm itself somehow knows what the methods on _ElementTree are so now autocomplete works. Unfortunately it seems that using .pyi files makes PyCharm forget this knowledge.
Here is documentation on type hinting in PyCharm.
And yes, in general you will have to get used to less autocompletion and general type information and static analysis. Fortunately I think there is a lot to make up for it that isn't possible in other languages :)
Install KITE, its a super fast auto suggest engine for python. It works for Pycharm,Sublime etc...
For more details view this youtube video
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.
I have written some documentation for a Python module with Sphinx. It builds and displays the HTML perfectly on my computer. However, I have been maintaining a Read the Docs version and that is not displaying properly. When I checked it when the docs were more sparse, it worked but it has since stopped.
The API page (linked) should have more methods detailed than it does. When I look at the raw .rst files on GitHub, they contain the information but it doesn't display on RTD.
I changed the folder structure for the project when I put it on PyPI, so I'm wondering whether it's that but I can't see how to fix it.
Any help would be greatly appreciated.
It turns out that I had used Requests without putting it in the requirements file. I just added it and now it builds fine.