德国大神写的Scribus日历插件

电脑技术 电脑技术 1135 人阅读 | 0 人回复 | 2022-04-15

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. # -*- coding: UTF-8 -*-

  2. """
  3. VERSION: 0.1    2022
  4. AUTHOR: RASCADINO.
  5. LICENSE: GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.

  8. Scribus Version >= 1.5.8 Mac

  9. Download Scribus 1.5.8 (scribus-devel)
  10. https://sourceforge.net/projects/scribus/files/scribus-devel/1.5.8/

  11. Nutzung:

  12. !!! Das Script wurde nur unter MacOs 12.x getestet.
  13. !!! Zum Verhalten in andern Betriebssystemen kann vom Autor keine Aussage getroffen werden.

  14. Aufruf im Terminal: /Applications/Scribus.app/Contents/MacOS/Scribus -g -ns -py /pfad/zu/deinem/kalendersript.py
  15. Aufruf in Scribus:  Menü 'Scrip' -> 'Script ausführen...

  16. Falls Module nicht vorhanden sind, können diese wie folgt installiert werden:
  17. /Applications/Scribus.app/Contents/Frameworks/Python.framework/Versions/3.9/bin/pip3 install modulname

  18. Kalenderbilder:
  19. Bilder numerisch nach den Monaten benennen: 01.jpg, ... 12.jpg
  20. Nur JPGs mit der Erweiterung . jpg
  21. Alle 12 Bilder in einen Ordner speichern

  22. Nach dem Scriptaufruf poppen Dialoge auf mit den Fragen nach:
  23.     - Kalenderformat, DIN A3 oder DIN A2
  24.     - Kalenderjahr
  25.     - Ordner der Kalenderbilder
  26.     - unter welchem Namen soll der Kalender gespeichert werden (gespeichert wird als Scribus *.sla)
  27.     - Speicherort
  28.     - Copyright Inhaber

  29. Achtung!
  30. Das Speichern der PDF-Datei kann je nach Maschine einige Zeit dauern. Wenn sich Scribus von selbst
  31. schließt ist der Vorgang in der Regel abgeschlossen. Etwas Geduld haben!
  32. """

  33. #imports
  34. import calendar
  35. from calendar import monthrange
  36. import locale
  37. import datetime
  38. from datetime import datetime, timedelta, date
  39. import platform
  40. import pandas as pd
  41. import sys
  42. import time

  43. try:
  44.     import scribus
  45. except ImportError:
  46.     print("Dieses Python-Script läuft nur innerhalb des Scribus scripting interface.")
  47.     print("Aufruf via Command Line oder direkt aus Scribus Menü Script -> Script ausführen..!")
  48.     sys.exit(1)

  49. os = platform.system()
  50. if os != "Darwin":
  51.     print("Dein Betriebssystem wird wahrscheinlich nicht unterstützt! Getestet nur nur für MAC OS.")
  52.     scribus.messageBox("OS error", "Dein Betriebssystem wird wahrscheinlich nicht unterstützt! Getestet nur für MAC OS.", scribus.ICON_INFORMATION)
  53.     #sys.exit(1)

  54. todays_date = date.today()
  55. nextYear = int(todays_date.year) + 1

  56. # Dialog-Boxen zur Personalisierung
  57. calFormat        = scribus.valueDialog('Kalenderformat', 'Kalenderformt DIN A3 ODER DIN A2', "A2")
  58. year            = scribus.valueDialog('Kalenderjahr', 'Bitte Kalenderjahr eingeben', str(nextYear))
  59. calName         = scribus.valueDialog('Name Deines Kalenders, ohne Erweiterung', 'Dateiname', 'meinTollerKalender')
  60. copyrightOwner  = scribus.valueDialog('Cpyright Inhaber', 'Copyright', 'Max Mustermann')
  61. calImagePath    = scribus.fileDialog('Ordner Deiner Kalenderbilder auswählen', "Bilder", isdir=1)
  62. calPath         = scribus.fileDialog('Ordner auswählen, in dem Dein Kalender gespeichert wird', "Ordner auswählen", isdir=1)
  63. calPathName     = calPath + "/" + calName + ".sla"
  64. year            = int(year)
  65. calImagePath    = calImagePath + "/"

  66. # Parameter
  67. calImageExt     = ".jpg"                                         # Dateierweiterung der Bilder
  68. fontDefault     = "SF Pro Text Ultralight"                       # https://github.com/sahibjotsaggu/San-Francisco-Pro-Fonts/blob/master/SF-Pro-Text-Ultralight.otf
  69. black           = "#282728"                                      # in Scribus anlegen CMYK
  70. cyan            = 'Cyan'
  71. grau            = '#7e7e7e'                                      # in Scribus anlegen CMYK
  72. copyright       = u"\xa9 " + str(year) + " " + copyrightOwner
  73. monat           = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"]
  74. jahrZeitAnfang  = [["2022", ["2022-03-20", "2022-06-21", "2022-09-23", "2022-12-21"]], ["2023", ["2023-03-20", "2023-06-21", "2023-09-23", "2023-12-21"]], ["2024", ["2024-03-20", "2024-06-20", "2024-09-22", "2024-12-21"]], ["2025", ["2025-03-20", "2025-06-21", "2025-09-22", "2025-12-21"]], ["2026", ["2026-03-20", "2026-06-21", "2026-09-22", "2026-12-21"]], ["2027", ["2027-03-20", "2027-06-21", "2027-09-23", "2027-12-22"]], ["2028", ["2028-03-20", "2028-06-20", "2028-09-23", "2028-12-21"]], ["2029", ["2029-03-20", "2029-06-21", "2029-09-22", "2029-12-21"]], ["2030", ["2030-03-20", "2030-06-21", "2030-09-22", "2030-12-21"]]]

  75. # Ab hier nichts mehr ändern!
  76. ###########################################################################################################################################################################
  77. if(calFormat == "A2"):
  78.     print("A2")
  79.     formatB         = 594           # DIN A2 Breite
  80.     formatH         = 420           # DIN A2 Höhe
  81.     datBoxB         = 13
  82.     datBoxH         = 8.5
  83.     datBoxStartX    = 13.6
  84.     datBoxY         = 400.5
  85.     datBoxFontSize  = 20
  86.     holBoxX         = 16.8
  87.     holBoxY         = 410.5
  88.     holBoxB         = 400
  89.     holBoxH         = 5
  90.     holFontSize     = 9
  91.     mBoxX           = 488
  92.     mBoxY           = 397.5
  93.     mBoxB           = 90
  94.     mBoxH           = 15
  95.     mBoxFontSize    = 30
  96.     lineBoxStartY   = 397.6
  97.     lineBoxEndY     = 407.1
  98.     lineBoxWidth    = 0.035
  99.     wnTextBoxY      = lineBoxStartY - 1
  100.     wnTextBoxB      = 6
  101.     wnTextBoxH      = 6
  102.     wnTextBoxFontSize = 10
  103.     iBoxX           = 17
  104.     iBoxY           = 17
  105.     iBoxB           = 560
  106.     iBoxH           = 560 / 1.5
  107.     tBoxXstart      = 10
  108.     tBoxB           = 143
  109.     tBoxH           = 143 / 1.5
  110.     tBoxY           = 53
  111.     yBoxX           = 0
  112.     yBoxY           = 364
  113.     yBoxB           = formatB
  114.     yBoxH           = 25
  115.     cBoxX           = 0
  116.     cBoxY           = 386
  117.     cBoxB           = formatB
  118.     cBoxH           = 6
  119. else:
  120.     print("A3")
  121.     formatB         = 420           # DIN A3 Breite
  122.     formatH         = 297           # DIN A3 Höhe
  123.     datBoxB         = 10.5
  124.     datBoxH         = 8.5
  125.     datBoxStartX    = 26
  126.     datBoxY         = 271
  127.     datBoxFontSize  = 18
  128.     holBoxX         = 28.25
  129.     holBoxY         = 286
  130.     holBoxB         = 285
  131.     holBoxH         = 5
  132.     holFontSize     = 9
  133.     mBoxX           = 362.25
  134.     mBoxY           = datBoxY
  135.     mBoxB           = 30
  136.     mBoxH           = 8.5
  137.     mBoxFontSize    = 18
  138.     lineBoxStartY   = 267.5
  139.     lineBoxEndY     = 277
  140.     lineBoxWidth    = 0.035
  141.     wnTextBoxY      = lineBoxStartY - 1
  142.     wnTextBoxB      = 6
  143.     wnTextBoxH      = 6
  144.     wnTextBoxFontSize = 10
  145.     iBoxX           = 28.15
  146.     iBoxY           = 17
  147.     iBoxB           = 363.9
  148.     iBoxH           = 242.6
  149.     tBoxXstart      = 10
  150.     tBoxY           = 40
  151.     tBoxB           = 100
  152.     tBoxH           = 100 / 1.5
  153.     yBoxX           = 0
  154.     yBoxY           = 258
  155.     yBoxB           = formatB
  156.     yBoxH           = 25
  157.     cBoxX           = 0
  158.     cBoxY           = 280
  159.     cBoxB           = formatB
  160.     cBoxH           = 6
  161. #sys.exit()
  162. #################################
  163. # functions
  164. #################################

  165. def feiertage(year):
  166.     A = year % 19
  167.     K = year // 100
  168.     M = 15 + (3 * K + 3) // 4 - ( 8 * K + 13) // 25
  169.     D = (19 * A + M) % 30
  170.     S = 2 - (3 * K + 3) // 4
  171.     R = D // 29 + (D // 28 - D // 29) * (A // 11)
  172.     OG = 21 + D + R
  173.     SZ = 7 - (year + year // 4 + S) % 7
  174.     OE = 7 - (OG - SZ) % 7
  175.     OS = (OG + OE)
  176.     if OS > 31:
  177.         os = str(year) + "-04-" + str(OS-31)
  178.         kf = str(year) + "-04-" + str(OS-33)
  179.         om = str(year) + "-04-" + str(OS-30)
  180.     else:
  181.         os = str(year) + "-04-" + str(OS)
  182.         kf = str(year) + "-04-" + str(OS-2)
  183.         om = str(year) + "-04-" + str(OS+1)
  184.     easter = [
  185.         [kf, "Karfreitag", 0],
  186.         [os, str("Ostersonntag"), 1],
  187.         [om, "Ostermontag", 1]
  188.     ]
  189.     oss = easter[1][0].split('-')
  190.     himf = datetime(int(oss[0]), int(oss[1]), int(oss[2])) + timedelta(days = 40)
  191.     himf = str(himf).split(' ')
  192.     easter.append([himf[0], "Christi Himmelfahrt", 1])
  193.     pfso = datetime(int(oss[0]), int(oss[1]), int(oss[2])) + timedelta(days = 49)
  194.     pfso = str(pfso).split(' ')
  195.     easter.append([pfso[0], "Pfingstsonntag", 1])
  196.     pfmo = datetime(int(oss[0]), int(oss[1]), int(oss[2])) + timedelta(days = 50)
  197.     pfmo = str(pfmo).split(' ')
  198.     easter.append([pfmo[0], "Pfingstmontag", 0])
  199.     frln = datetime(int(oss[0]), int(oss[1]), int(oss[2])) + timedelta(days = 60)
  200.     frln = str(frln).split(' ')
  201.     easter.append([frln[0], "Fronleichnam", 0])
  202.     rosm = datetime(int(oss[0]), int(oss[1]), int(oss[2])) - timedelta(days = 48)
  203.     rosm = str(rosm).split(' ')
  204.     easter.append([rosm[0], "Rosenmontag", 0])
  205.     sont = datetime(year, 12, 24).weekday()
  206.     if sont != 6:           # 24.12. ist kein Sonntag
  207.         diff4 = sont + 1
  208.         adv4 = datetime(year, 12, 24) - timedelta(int(diff4))
  209.         adv4 = str(adv4).split(' ')
  210.         diff3 = diff4 + 7
  211.         adv3 = datetime(year, 12, 24) - timedelta(int(diff3))
  212.         adv3 = str(adv3).split(' ')
  213.         diff2 = diff4 + 14
  214.         adv2 = datetime(year, 12, 24) - timedelta(int(diff2))
  215.         adv2 = str(adv2).split(' ')
  216.         diff1 = diff4 + 21
  217.         adv1 = datetime(year, 12, 24) - timedelta(int(diff1))
  218.         adv1 = str(adv1).split(' ')
  219.     else:                   # 24.12. ist Sonntag
  220.         adv4 = str(year) + '-12-24'
  221.         adv3 = datetime(year, 12, 24) - timedelta(7)
  222.         adv3 = str(adv3).split(' ')
  223.         adv2 = datetime(year, 12, 24) - timedelta(14)
  224.         adv2 = str(adv2).split(' ')
  225.         adv1 = datetime(year, 12, 24) - timedelta(21)
  226.         adv1 = str(adv1).split(' ')
  227.     easter.append([adv1[0], "1. Advent", 0])
  228.     easter.append([adv2[0], "2. Advent", 0])
  229.     easter.append([adv3[0], "3. Advent", 0])
  230.     easter.append([adv4[0], "4. Advent", 0])
  231.     easter.append([str(year) + "-12-24", "Heiligabend", 0])
  232.     easter.append([str(year) + "-12-25", "Weihnachten", 1])
  233.     easter.append([str(year) + "-12-26", "2. Weihnachtstag", 1])
  234.     easter.append([str(year) + "-01-01", "Neujahr", 1])
  235.     easter.append([str(year) + "-02-14", "Valentinstag", 0])
  236.     easter.append([str(year) + "-05-01", "Tag der Arbeit", 1])
  237.     easter.append([str(year) + "-08-15", "Mariä Himmelfahrt", 0])
  238.     easter.append([str(year) + "-10-03", "Tag der Deutschen Einheit", 1])
  239.     for x in jahrZeitAnfang:
  240.         if x[0] == str(year):
  241.             easter.append([x[1][0], "Frühlingsanfang", 0])
  242.             easter.append([x[1][1], "Sommeranfang", 0])
  243.             easter.append([x[1][2], "Herbstanfang", 0])
  244.             easter.append([x[1][3], "Winteranfang", 0])
  245.     return(easter)

  246. def daysInMonth(year):
  247.     dim = []
  248.     for m in range(1, 13):
  249.         dim.append(monthrange(year, m)[1])
  250.     return (dim)

  251. def compCal(year):
  252.     cal = []
  253.     m = 1
  254.     dpm = daysInMonth(year)
  255.     for d in dpm:
  256.         n = m - 1
  257.         cal.append([m])
  258.         for r in range(1, d + 1):
  259.             dat = str(year) + "-" + str(m).zfill(2) + "-" + str(r).zfill(2)     # Datum
  260.             ts = pd.Timestamp(dat)
  261.             dna = ts.dayofweek                                                  # Wochentag
  262.             d1 = ""
  263.             d2 = 0
  264.             d3 = ""
  265.             for f in feiertage(year):                                           # Feiertag?
  266.                 if str(dat) == str(f[0]):
  267.                     d1 = str(f[1])
  268.                     d2 = str(f[2])
  269.             ts = pd.Timestamp(int(year), int(m), int(r))
  270.             d3 = str(ts.week).zfill(2)
  271.             cal[n].append([dat, dna, d1, d2, d3])                               # [monat[datum, wochentag, feiertagname, bundesweit, kalenderwoche], ...], ...
  272.         m += 1
  273.     return(cal)

  274. ############################
  275. # Scribus Code
  276. ############################

  277. def baueKalender(year):
  278.     calenda = compCal(year)
  279.     #print(calenda)
  280.     #sys.exit()
  281.     scribus.newDocument((formatH, formatB), (0, 0, 0, 0), scribus.LANDSCAPE, 1, scribus.UNIT_MILLIMETERS, scribus.PAGE_1, 12, 1)
  282.     scribus.createLayer("Bilder")
  283.     scribus.createLayer("Text")
  284.     for z in range(0, 11):
  285.         scribus.newPage(-1)

  286.     for p in range(scribus.pageCount()):
  287.         m  =  p + 1
  288.         print("Baue Blatt " + str(m))
  289.         scribus.gotoPage(m)
  290.         fText = ""
  291.         for n in range(len(calenda[p])):
  292.             if len(str(calenda[p][n])) > 2:
  293.                 if len(str(calenda[p][n][2])) > 0:
  294.                     part = str(calenda[p][n][0]).split('-')
  295.                     fDat = part[2] + "." + part[1] + ". "
  296.                     fText = fText + str(fDat) + str(calenda[p][n][2]) + " " + u"\xb7 "
  297.                 dat = str(calenda[p][n][0]).split("-")
  298.                 if n > 1:
  299.                     datBoxX = (datBoxB * (n - 1)) + datBoxStartX
  300.                 else:
  301.                     datBoxX = datBoxStartX
  302.                 dBox = scribus.createText(datBoxX, datBoxY, datBoxB, datBoxH)
  303.                 scribus.setText(dat[2], dBox)
  304.                 scribus.setFont(fontDefault, dBox)
  305.                 scribus.setFontSize(datBoxFontSize, dBox)
  306.                 scribus.setTextAlignment(scribus.ALIGN_CENTERED, dBox)
  307.                 scribus.sendToLayer("Text" , dBox)
  308.                 if int(calenda[p][n][1]) > 5 or int(calenda[p][n][3]) > 0:
  309.                     color = cyan
  310.                 else:
  311.                     color = black
  312.                 scribus.setTextColor(color, dBox)
  313.                 #Wochennummer
  314.                 if int(calenda[p][n][1]) < 1:
  315.                     # Trennlinie
  316.                     wnLine = scribus.createLine(datBoxX, lineBoxStartY, datBoxX, lineBoxEndY)
  317.                     scribus.setLineWidth(lineBoxWidth, wnLine)
  318.                     scribus.setLineColor(grau, wnLine)
  319.                     scribus.sendToLayer("Text", wnLine)
  320.                     # Wochennummer
  321.                     wnTxt = scribus.createText(datBoxX + 1, wnTextBoxY, wnTextBoxB, wnTextBoxH)
  322.                     scribus.setText(str(calenda[p][n][4]), wnTxt)
  323.                     scribus.setFont(fontDefault, wnTxt)
  324.                     scribus.setFontSize(wnTextBoxFontSize, wnTxt)
  325.                     scribus.setTextColor(grau, wnTxt)
  326.                     scribus.setTextAlignment(scribus.ALIGN_CENTERED, wnTxt)
  327.                     scribus.sendToLayer("Text", wnTxt)
  328.         # Zeile Feiertage
  329.         fText = fText[:-3]
  330.         fBox = scribus.createText(holBoxX, holBoxY, holBoxB, holBoxH)
  331.         scribus.setText(fText, fBox)
  332.         scribus.setFont(fontDefault, fBox)
  333.         scribus.setFontSize(holFontSize, fBox)
  334.         scribus.setTextColor(grau, fBox)
  335.         scribus.setTextAlignment(scribus.ALIGN_LEFT, fBox)
  336.         # Zeile Monat Jahr
  337.         mBox = scribus.createText(mBoxX, mBoxY, mBoxB, mBoxH)
  338.         if calFormat == "A2":
  339.             scribus.setText(monat[p] + " " + u"\xb7" + " " + str(year), mBox)
  340.         else:
  341.             scribus.setText(str(m).zfill(2) + " " + u"\xb7" + " " + str(year), mBox)
  342.         scribus.setFont(fontDefault, mBox)
  343.         scribus.setFontSize(mBoxFontSize, mBox)
  344.         scribus.setTextColor(black, mBox)
  345.         scribus.setTextAlignment(scribus.ALIGN_RIGHT, mBox)
  346.         # Bilder
  347.         iBox = scribus.createImage(iBoxX, iBoxY, iBoxB, iBoxH)
  348.         imgName = str(m).zfill(2) + str(calImageExt)
  349.         scribus.loadImage( calImagePath + imgName, iBox)
  350.         scribus.setScaleImageToFrame(1,1,iBox)
  351.         scribus.sendToLayer("Bilder" , iBox)
  352.     # Deckblatt
  353.     print("Baue Deckblatt")
  354.     scribus.newPage(1)
  355.     # Hintergrund
  356.     tBackGroud = scribus.createRect(10, tBoxY, tBoxB * 4, tBoxH * 3)
  357.     scribus.setFillColor(grau, tBackGroud)
  358.     scribus.sendToLayer("Bilder", tBackGroud)
  359.     scribus.selectObject(tBackGroud)
  360.     scribus.moveSelectionToBack()
  361.     scribus.deselectAll()
  362.     # Bilder
  363.     for i in range(0, 12):
  364.         if i < 4:                           # 1. Reihe Bilder
  365.             xt = tBoxXstart + i * tBoxB
  366.             yt = tBoxY
  367.         elif i > 3 and i < 8:               # 2. Reihe Bilder
  368.             xt = 10 + (i - 4) * tBoxB
  369.             yt = tBoxY + tBoxH
  370.         elif i > 7:                         # 3. Reihe Bilder
  371.             xt = 10 + (i - 8) * tBoxB
  372.             yt = tBoxY + 2 * tBoxH
  373.         z = i + 1
  374.         iBox = scribus.createImage(xt, yt, tBoxB, tBoxH)
  375.         tName = str(z).zfill(2) + str(calImageExt)
  376.         scribus.loadImage( calImagePath + tName, iBox)
  377.         scribus.setScaleImageToFrame(1,0,iBox)
  378.         scribus.setScaleImageToFrame(0,1,iBox)
  379.         scribus.sendToLayer("Bilder" , iBox)
  380.     # Jahreszahl
  381.     yBox = scribus.createText(yBoxX, yBoxY, yBoxB, yBoxH)
  382.     scribus.setText(str(year), yBox)
  383.     scribus.setFont(fontDefault, yBox)
  384.     scribus.setFontSize(50, yBox)
  385.     scribus.setTextColor(black, yBox)
  386.     scribus.setTextAlignment(scribus.ALIGN_CENTERED, yBox)
  387.     # copyright
  388.     cBox = scribus.createText(cBoxX, cBoxY, cBoxB, cBoxH)
  389.     scribus.setText(copyright, cBox)
  390.     scribus.setFont(fontDefault, cBox)
  391.     scribus.setFontSize(12, cBox)
  392.     scribus.setTextColor(black, cBox)
  393.     scribus.setTextAlignment(scribus.ALIGN_CENTERED, cBox)
  394.     # Dokument speichern (.sla)
  395.     scribus.saveDocAs(calPathName)
  396.     # PDF
  397.     pdf = scribus.PDFfile()
  398.     pdf.file = calPath + "/" + calName + ".pdf"
  399.     pdf.save()

  400. # Script Auftruf
  401. ######################################################
  402. def main():
  403.     """ Application/Dialog loop with Scribus sauce around """
  404.     try:
  405.         scribus.statusMessage('Running script...')
  406.         scribus.progressReset()
  407.         app = baueKalender(year)
  408.     finally:
  409.         if scribus.haveDoc() > 0:
  410.             scribus.redrawAll()
  411.         scribus.statusMessage('Done.')
  412.         scribus.progressReset()

  413. if __name__ == '__main__':
  414.     main()
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则