浏览代码

- Leere PDF-Dateien erkennen
- powerplay.py einzelner Bericht
- transformer7.py

Robert Bedner 4 年之前
父节点
当前提交
b5acda7926
共有 9 个文件被更改,包括 222 次插入68 次删除
  1. 31 0
      gctools/create-cube.mac
  2. 二进制
      gctools/create-cube.mcx
  3. 36 9
      gctools/portal.py
  4. 24 0
      gctools/powerplay.py
  5. 0 35
      gctools/publish-reports.mac
  6. 68 0
      gctools/publish-single-report.mac
  7. 二进制
      gctools/publish-single-report.mcx
  8. 34 24
      gctools/transformer7.py
  9. 29 0
      pdf-test.py

+ 31 - 0
gctools/create-cube.mac

@@ -0,0 +1,31 @@
+Dim objApp As Object
+Dim objModel As Object
+Dim objCube As Object
+
+Dim filename as String
+Dim folder as String
+Dim publish As String
+
+
+Sub Main ()
+   Rem On Error Resume Next
+
+   rem folder = "C:\GAPS\Portal\System\Models"
+   rem filename = "belege.mdc"
+   rem publish = "C:\GAPS\Portal\System\Cube_out\temp"
+   
+   folder = GetField(Command, 1, ",")
+   filename = GetField(Command, 2, ",")
+   publish = GetField(Command, 3, ",")
+   
+   Set objApp = CreateObject("CognosTransformer.Application")
+   Set objModel = objApp.OpenModel(folder + "\" + filename)
+   Set objCube = objModel.Cubes.Item(1)
+
+   objCube.MDCFile = publish + "\" + filename + ".mdc"
+   objCube.Update
+   objCube.CreateMDCFile
+   
+   objModel.Close
+   objApp.Quit
+End Sub

二进制
gctools/create-cube.mcx


+ 36 - 9
gctools/portal.py

@@ -1,7 +1,10 @@
+import plac
 import subprocess
+import pandas as pd
 import time
 from datetime import datetime
 from pathlib import Path
+from shutil import copy2
 import config
 
 cfg = config.config()
@@ -9,18 +12,19 @@ report_dir = cfg.portal_dir + '\\System\\Report'
 publish_dir = cfg.portal_dir + '\\daten'
 
 
-def portal(portal_file):
-    print(f"== Portal '{portal_file}.xml' ==")
-    full_filename = f'{cfg.xml_dir}\\{portal_file}.xml'
+@plac.pos('config_file', '', type=str)
+def portal(config_file='GAPS_neu'):
+    print(f"== Portal '{config_file}.xml' ==")
+    full_filename = f'{cfg.xml_dir}\\{config_file}.xml'
     if not Path(full_filename).exists():
         print(f"!! Datei '{full_filename}' existiert nicht !!")
         return
-    prepare_report_temp()
-    pub_dir = Path(f'{publish_dir}\\{portal_file}')
+    prepare_report_temp(config_file)
+    pub_dir = Path(f'{publish_dir}\\{config_file}')
     if not pub_dir.exists():
         pub_dir.mkdir()
 
-    with open(f'{cfg.log_dir}\\{portal_file}.mac.log', 'w') as logfile:
+    with open(f'{cfg.log_dir}\\{config_file}.mac.log', 'w') as logfile:
         export_files('jpg', pub_dir, logfile)
         export_files('xls', pub_dir, logfile)
         export_files('pdf', pub_dir, logfile)
@@ -66,9 +70,32 @@ def cleanup_dir(pub_dir):
         f.unlink()
 
 
-def prepare_report_temp():
-    pass
+def prepare_report_temp(config_file):
+    df = pd.read_csv(cfg.xml_dir + '\\info\\reports.csv', sep=';')
+    df = df[df['Datei'].str.contains(config_file + '\\.', case=False)]
+    format_list = [('GIF', 'jpg'), ('PDF', 'pdf'), ('XLS', 'xls')]
+
+    for (filter_col, export_format) in format_list:
+        reports = set(df[df[filter_col] == 'J']['Report'].to_list())
+        rep_dir = f'{report_dir}\\{config_file}\\{export_format}'
+        unlink_and_recreate(Path(rep_dir))
+
+        for rep in reports:
+            ppx = report_dir + '\\' + rep + '.ppx'
+            ppr = report_dir + '\\' + rep + '.ppr'
+            if Path(ppx).exists():
+                copy2(ppx, rep_dir)
+            elif Path(ppr).exists():
+                copy2(ppr, rep_dir)
+
+
+def unlink_and_recreate(dirname):
+    if not dirname.exists():
+        dirname.mkdir(parents=True)
+    else:
+        for f in dirname.glob('*.pp[xr]'):
+            f.unlink()
 
 
 if __name__ == '__main__':
-    portal('GAPS_neu')
+    plac.call(portal)

+ 24 - 0
gctools/powerplay.py

@@ -0,0 +1,24 @@
+import plac
+import subprocess
+import config
+
+cfg = config.config()
+report_dir = cfg.portal_dir + '\\System\\Report'
+publish_dir = cfg.portal_dir + '\\daten'
+
+
+@plac.pos('report_file', '', type=str)
+@plac.opt('export_format', '', type=str)
+@plac.opt('publish_subdir', '', type=str)
+def export(report_file, export_format='pdf', publish_subdir=None):
+    pub_dir = publish_dir if publish_subdir is None else publish_dir + '\\' + publish_subdir
+    cmd = f'"{cfg.cognos_dir}\\runmac32.exe" "{cfg.tools_dir}\\publish-single-report.mac" "{report_dir}","{report_file}","{export_format}","{pub_dir}"'
+    print(f"Exportiere '{report_file}' als '{export_format}' nach '{pub_dir}'...", end='')
+
+    p = subprocess.Popen(cmd)
+    p.wait()
+    print('erfolgreich.')
+
+
+if __name__ == '__main__':
+    plac.call(export)

+ 0 - 35
gctools/publish-reports.mac

@@ -69,38 +69,3 @@ Sub Main ()
    Loop
    objApp.Quit
 End Sub
-
-
-Sub SaveAsXls (report As String)
-   Dim objPPRep as Object
-   Dim objPPRes as Object
-   Set objPPRep = GetObject(, "CognosPowerPlay.Report")
-   Set objPPRes = objPPRep.Columns.CumPercentOfBase _
-   (objPPRep.Rows.Item("1996"))
-   objPPRep.SaveAs "Mein neuer Bericht"
-   Set objPPRep = Nothing
-End Sub
-
-
-Sub Pdf()
-   Dim objPDF as Object
-   Dim objPPRep as Object
-   Set objPPRep = CreateObject ("CognosPowerPlay.Report")
-   objPPRep.Open( "C:\COGNOS\BEISPIEL.PPR" )
-   objPPRep.visible( TRUE )
-   Set objPDF = objPPRep.PDFFile( "C:\COGNOS\PDFBEISPIEL" , True )
-   With objPDF
-      .SaveEntireReport = False
-      .AxisOnAllPages = True
-      .ChartTitleOnAllPages = False
-      .IncludeLegend = True
-      .SetChartToSave objPPRep.Graphs.Item( 1 )
-      .SetListOfLayersToSave objPPRep.Layers
-      .SetListOfRowsToSave objPPRep.Rows
-   End With
-   objPDF.Save
-   Set objPPRep = Nothing
-   Set objPDF = Nothing
-End Sub
-
-

+ 68 - 0
gctools/publish-single-report.mac

@@ -0,0 +1,68 @@
+Dim objApp As Object
+Dim objRep As Object
+Dim filename as String
+Dim folder as String
+Dim publish As String
+Dim exportFormat As String
+
+Sub Main ()
+   Rem On Error Resume Next
+   Set objApp = CreateObject("CognosPowerPlay.Application")
+   rem folder = "C:\GAPS\Portal\System\Report"
+   rem filename = "test.ppr"
+   rem exportFormat = "pdf"
+   rem publish = "C:\GAPS\Portal\daten"
+   
+   folder = GetField(Command, 1, ",")
+   filename = GetField(Command, 2, ",")
+   exportFormat = GetField(Command, 3, ",")
+   publish = GetField(Command, 4, ",")
+
+   Dim exportFile as String
+   Dim objPDF as Object
+   
+   If LCase(Right(filename, 4)) = ".ppr" Or LCase(Right(filename, 4)) = ".ppx" Then
+      Set objRep = CreateObject("CognosPowerPlay.Report")
+      objRep.Open(folder + "\" + filename)
+      exportFile = publish + "\" + Left(filename, Len(filename) -4)
+
+      Select Case exportFormat
+         Case "pdf"
+            
+            Set objPDF = objRep.PDFFile(exportFile + "_0.pdf", True)
+            With objPDF
+               .SaveEntireReport = True
+               .AxisOnAllPages = True
+               .ChartTitleOnAllPages = False
+               .IncludeLegend = False
+            End With
+            objPDF.Save
+
+            For i = 1 to objRep.Layers.Count
+               Set objPDF = objRep.PDFFile(exportFile + "_" + i + ".pdf", True)
+               With objPDF
+                  .SaveEntireReport = False
+                  .SaveAllCharts = False                     
+                  .AxisOnAllPages = True
+                  .ChartTitleOnAllPages = False
+                  .IncludeLegend = False
+                  .SetListOfLayersToSave objRep.Layers.Subset(i, i)
+                  .SetListOfRowsToSave objRep.Rows
+               End With
+               objPDF.Save
+            Next
+
+         Case "asc"
+            objRep.SaveAs exportFile, 3
+         Case "xls"
+            objRep.SaveAs exportFile, 4
+         Case "ppx"
+            objRep.SaveAs exportFile, 5
+         Case Else
+            objRep.Publish publish, False, True, True    
+      End Select
+      objRep.Close
+   End if
+
+   objApp.Quit
+End Sub

二进制
gctools/publish-single-report.mcx


+ 34 - 24
gctools/transformer7.py

@@ -1,27 +1,37 @@
-# import logging
-import win32com.client
-from pathlib import Path
-
-
-class transformer7:
-    def __init__(self):
-        self.app = win32com.client.Dispatch('CognosPowerPlay.Report')   # "CognosTransformer.Application")
-        print(self.app)
-        self.app.visible = 1
-        print(self.app.Version)
-
-    def open(self, file, format=None):
-        filename = Path(file).resolve()
-        # path = str(filename.parent.resolve())
-        print(filename)
-        try:
-            model = self.app.OpenModel(filename)
-            cube = model.Cubes.Item(1)
-            cube.Update()
-            model.CreateMDCFiles()
-        except Exception as e:
-            print(e)
+import plac
+import subprocess
+import config
+import re
+
+cfg = config.config()
+models_dir = cfg.portal_dir + "\\System\\Models"
+cube_out = cfg.portal_dir + "\\System\\Cube_out"
+cube_temp = cube_out + "\\temp"
+
+
+@plac.pos('model_file', '', type=str)
+def create(model_file):
+    cube = cube_name(model_file)
+
+    cmd = f'"{cfg.cognos_dir}\\runmac32.exe" "{cfg.tools_dir}\\create-cube.mac" "{models_dir}","{model_file}","{cube_temp}"'
+    print(f"Erstelle Cube '{cube}' fuer '{model_file}'...", end='')
+
+    p = subprocess.Popen(cmd)
+    p.wait()
+    temp_file = f'{cube_temp}\\{model_file}.mdc'
+    cube_file = f'{cube_out}\\{cube}.mdc'
+
+    print('erfolgreich.')
+
+
+def cube_name(model_file):
+    p = re.compile(r'cube_out\\([^\.]+)\.mdc', re.IGNORECASE)
+    with open(models_dir + '\\' + model_file, 'r', errors='ignore') as f:
+        for line in f.readlines():
+            m = p.search(line)
+            if m:
+                return p.match(m.group()).groups()[0].lower()
 
 
 if __name__ == '__main__':
-    transformer7().open('Model\\NW_GW_VK.pyi')
+    plac.call(create)

+ 29 - 0
pdf-test.py

@@ -0,0 +1,29 @@
+import pdfplumber
+import re
+import json
+from pathlib import Path
+from datetime import datetime, timedelta
+
+today = datetime.now()
+yesterday = today - timedelta(days=1)
+current_date = [today.strftime('%d.%m.%Y'), today.strftime('%d/%m/%Y'), yesterday.strftime('%d.%m.%Y'), yesterday.strftime('%d/%m/%Y')]
+errors = {
+    'empty': [],
+    'outdated': []
+}
+
+files = [f for f in Path('C:\\Projekte\\Timmermanns\\leer').glob('*.pdf')]
+files_count = len(files)
+
+for i, f in enumerate(files):
+    print(f'({i}/{files_count}) {f.name}                 ', end='\r')
+    with pdfplumber.open(str(f)) as pdf:
+        text = pdf.pages[0].extract_text()
+        if re.search(r'\d+ von \d+$', text):
+            errors['empty'].append(f.name)
+        report_date = re.search(r'\d{2}[\./]\d{2}[\./]\d{4}', text.split('\n')[0])
+        if report_date is not None and report_date.group() not in current_date:
+            errors['outdated'].append([f.name, report_date.group()])
+
+print()
+print(json.dumps(errors, indent=2))