Save Contents of QTextEdit as *.pdf? - python

I am trying to save the contents of a Text Editor as a pdf file. The text Editor has been made using PyQt (i didn't make the text Editor), i got the code of the text editor from here. I have done some changes to the editor but that wont be a problem.
After some initial research i found that i need to use ReportLab to publish a pdf file.But i can't find a way to do this .
Does anyone know how this could be accomplished ?

The source code for the Text Editor already has a PDF method, but it is unused, and possibly won't work properly as it stands.
A basic re-write of the method that should work on all platforms, would look like this:
def SavetoPDF(self):
filename = QtGui.QFileDialog.getSaveFileName(self, 'Save to PDF')
if filename:
printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)
printer.setPageSize(QtGui.QPrinter.A4)
printer.setColorMode(QtGui.QPrinter.Color)
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
printer.setOutputFileName(filename)
self.text.document().print_(printer)
The only other thing you'll need is a menu item to run it, so in Main.initUI just add:
pdfAction = QtGui.QAction("Save to PDF", self)
pdfAction.setStatusTip("Save to PDF")
pdfAction.triggered.connect(self.SavetoPDF)
...
file.addAction(pdfAction)

Related

Is it possible to insert text in a existing .otp file through Python?

I'm looking for a way/module to insert a string of text formatted by position and text size, into an already existing .otp file(OpenDocument Presentation Template/Libre&OpenOffice version of a .ppt file). Preferably through Python. I've tried googling but the only thing I find is about macros and I'm not sure if that will work the way I want it to.
As an example I'm looking for an end product like this, where the text is inserted through running a Python script.
I imagine the outline of the script to look something like:
import modules
file = 'cat.otp'
open file
some function for inserting formatted text(right size, text size etc) in otp file
you wrote .opt in text and in pseudo-code .otp, but extension should be .odp for a presentation file. otp is for a master. If you just want to add content this should work for both:
While there is a convenient lib for pptx: python-pptx I don't know one for OpenOffice-Files.
If you don't have to create from scratch but just edit something (and it sounds like that) you may unpack the file via zipfile, edit the content.xml and zip back the whole file structure to odp-file. If you take your demo-file and read the xml (use notepad++ with XML-Tools extension and pretty print function to de-linearize) it is quite self-explaining, you'll get an idea pretty fast.

Creating a script in python to alter the text in an SVG file

I'm new to python and I'm trying to create a script to open an existing .svg file, change the text/font/colour/size, and then save it as a separate .svg
I've managed to create another script using svgwrite but this can only make new files instead of editing existing ones.
I've had a look at a few other threads suggesting using the inkscape extension inkex.py but I can't figure out how to write anything working with this.
I've also tried lxml but I don't really understand what I'm doing with this. From everything I found I know I have to parse the .svg and from this I got the below code but this seems to be a dead end as the result is blank
import lxml.etree as ET
xml = ET.parse('C:\\Users\\Admin\\Desktop\\Test2.svg')
svg = xml.getroot()
print(svg)
print(svg.findall(".//{Element {http://www.w3.org/2000/svg}svg at 0x3e107a8"))
--Edit
In case anyone is trying to do something similar I did some more digging and found the best way to do it for my purpose was to open the svg, replace the string where it was needed and write to file, e.g.
Change = open(/files/file.svg, "rt")
data = change.read()
data = data.replace('original text', 'new text')
Change.close()
Change = open(/files/file.svg "wt")
Change.write(data)
Change.close()

How to read and print the contents of a ttf file?

Is there any way that I can open, read and write a ttf file?
Example:
with open('xyz.ttf') as f:
content = f.readline()
print(content)
A bit more:
If I open a .ttf (font) file with windows font viewer we see the following image
From this I like to extract following lines as text, with proper style.
What is exactly inside this file with *.ttf extension. I think you need to add more details of the input and output. If you reffering to a font type database you must first find a module/package to open and read it, since *.ttf isn't a normal text file.
Read the given links and install the required packages first:
https://pypi.python.org/pypi/FontTools
Then, as suggested:
from fontTools.ttLib import TTFont
font = TTFont('/path/to/font.ttf')
print(font)
<fontTools.ttLib.TTFont object at 0x10c34ed50>
If you need help with something else trying putting the input and expected output.
Other links:
http://www.starrhorne.com/2012/01/18/how-to-extract-font-names-from-ttf-files-using-python-and-our-old-friend-the-command-line.html
Here is a another useful python script:
https://gist.github.com/pklaus/dce37521579513c574d0

How to get contents of file in Sublime Text 3 Python API

I'm very new to Python and Sublime Text API dev... so this may be easy?
I want to display the contents of a file (that sits next to the currently open file) to a new panel window.
I can create a new panel no problem and get it to display a string using
def newLogWindow(self, output):
window = self.view.window()
new_view = window.create_output_panel("log")
new_view.run_command('erase_view')
new_view.run_command('append', {'characters': output})
window.run_command("show_panel", {"panel": "output.log"})
sublime.status_message('Metalang')
pass
But what I need is a function to get contents of file to pass to that function.
content = xxxx.open_file("filename.txt")
// somehow get contents of this file?
// pass it to log window
self.newLogWindow(content);
Thanks for your help!
In Sublime Text, the built in API to open a file is tied to the Window and will return a View that corresponds to a tab. In your case, you want to update a panel (an existing View that doesn't relate to a tab) with the contents of the file, so the Sublime Text API can't be used for this.
Instead you can do it directly in Python using the open method:
with open('filename.txt', 'r') as myfile:
content = myfile.read()
self.newLogWindow(content)

How to lock contents in wxTextCtrl?

I am trying to implement a simple application. It use wx.FileDialog to select a file, then display the file name in a wx.TextCtrl component.
It works allright at first, the file name was displayed as expected. However, when the mouse move over the text control component, the contents are gone.
Here is my code:
fdlg_input_dir = wx.FileDialog(dlg_input, "Choose input file", os.getcwd(), "", "All files(*.*)|*.*", wx.OPEN)
fdlg_input_dir.ShowModal()
textctrl_input_dir = wx.TextCtrl(dlg_input, 5, fdlg_input_dir.GetPath(), size=(300,20), pos=(85,20))
So my questions is, how to remain the contents in the TextCtrl component while the mouse moves over it?
Any suggestions are appreciated. Thanks.
Problem solved. It turns out that the initial text in the control component doesn't stand. Instead, using following code should archieve what I expected:
textctrl_input_dir.write(fdlg_input_dir.GetPath())

Categories

Resources