passwd-as-odt.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2007 Søren Roug, European Environment Agency
  4. #
  5. # This is free software. You may redistribute it under the terms
  6. # of the Apache license and the GNU General Public License Version
  7. # 2 or at your option any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public
  15. # License along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. #
  18. # Contributor(s):
  19. #
  20. from odf.opendocument import OpenDocumentText
  21. from odf.style import Style, TextProperties, ParagraphProperties, TableColumnProperties
  22. from odf.text import P
  23. from odf.table import Table, TableColumn, TableRow, TableCell
  24. PWENC = "utf-8"
  25. textdoc = OpenDocumentText()
  26. # Create a style for the table content. One we can modify
  27. # later in the word processor.
  28. tablecontents = Style(name="Table Contents", family="paragraph")
  29. tablecontents.addElement(ParagraphProperties(numberlines="false", linenumber="0"))
  30. textdoc.styles.addElement(tablecontents)
  31. # Create automatic styles for the column widths.
  32. # We want two different widths, one in inches, the other one in metric.
  33. # ODF Standard section 15.9.1
  34. widthshort = Style(name="Wshort", family="table-column")
  35. widthshort.addElement(TableColumnProperties(columnwidth="1.7cm"))
  36. textdoc.automaticstyles.addElement(widthshort)
  37. widthwide = Style(name="Wwide", family="table-column")
  38. widthwide.addElement(TableColumnProperties(columnwidth="1.5in"))
  39. textdoc.automaticstyles.addElement(widthwide)
  40. # Start the table, and describe the columns
  41. table = Table()
  42. table.addElement(TableColumn(numbercolumnsrepeated=4,stylename=widthshort))
  43. table.addElement(TableColumn(numbercolumnsrepeated=3,stylename=widthwide))
  44. f = open('/etc/passwd')
  45. for line in f:
  46. rec = line.strip().split(":")
  47. tr = TableRow()
  48. table.addElement(tr)
  49. for val in rec:
  50. tc = TableCell()
  51. tr.addElement(tc)
  52. p = P(stylename=tablecontents,text=unicode(val,PWENC))
  53. tc.addElement(p)
  54. textdoc.text.addElement(table)
  55. textdoc.save("passwd.odt")