Skip to content

Instantly share code, notes, and snippets.

@dbader
Created March 15, 2017 09:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbader/8006381c5ca340274936ff0d5b1b2942 to your computer and use it in GitHub Desktop.
Save dbader/8006381c5ca340274936ff0d5b1b2942 to your computer and use it in GitHub Desktop.
# LetterStart python script
import uno, datetime
#from datetime import date
# a UNO struct later needed to create a document
###############################################################################
def createDoc():
"""creates a new writer document and prints my name and address at the top right """
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star .frame.Desktop",ctx)
# open a writer document
doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )
# print name, address and date
text = doc.Text
cursor = text.createTextCursor()
tabs = "\t" * 10
########################################
# this works ONLY if the year is 4 digits
# and the day and month are 2 digits
today = str(datetime.datetime.now ().date())
lastno = len(today)
year = today[0 : 4]
month = today[5 : 7]
day = today[8 : lastno]
todaydate = day + "/" + month + "/"+ year
########################################
#today = http://datetime.datetime.now ()
#print(today + "now")
text.insertString( cursor, tabs + "John Doe\n", 0 )
text.insertString( cursor, tabs + "1st line of address\n" , 0 )
text.insertString( cursor, tabs + "2nd line\n" , 0 )
text.insertString( cursor, tabs + "3rd line\n" , 0 )
text.insertString( cursor, tabs + "more address or postcode\n" , 0 )
text.insertString(cursor, "\n" , 0)
text.insertString( cursor, tabs + todaydate + "\n" , 0 )
###############################################################################
#
# --- Refactored version below ---
#
"""
LetterStart python script
"""
import datetime
import uno
TABS = "\t" * 10
def insert_text(doc_text, cursor, text, indent=True):
if indent:
text = TABS + text + "\n"
else:
text = text + "\n"
doc_text.insertString(cursor, text, 0)
def create_doc():
"""
Creates a new writer document and prints my name
and address at the top right.
"""
ctx = uno.getComponentContext()
desktop = ctx.ServiceManager.createInstanceWithContext(
"com.sun.star.frame.Desktop", ctx)
# open a writer document
doc = desktop.loadComponentFromURL(
"private:factory/swriter", "_blank", 0, tuple())
doc_text = doc.Text
cursor = doc_text.createTextCursor()
today_date = datetime.date.today().strftime("%d/%m/%Y")
insert_text(doc_text, cursor, "John Doe")
insert_text(doc_text, cursor, "1st line of address")
insert_text(doc_text, cursor, "2nd line")
insert_text(doc_text, cursor, "3rd line")
insert_text(doc_text, cursor, "more address or postcode")
insert_text(doc_text, cursor, "", indent=False)
insert_text(doc_text, cursor, today_date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment