瀏覽代碼

- gctools Nachbau von Tasks
- refresh.py spontan für Timmermanns
- portal.py nahezu vollständig

Planung
- file-export.py angepasst - Plan über Saison passt jetzt

Robert Bedner 4 年之前
父節點
當前提交
07ad88a37d
共有 14 個文件被更改,包括 241 次插入307 次删除
  1. 二進制
      Excel/Dash_LH_Wien.xls
  2. 1 265
      Planung/V1.csv
  3. 19 19
      Planung/V1.json
  4. 2 2
      file-export.py
  5. 7 0
      gctools/config.py
  6. 4 0
      gctools/logger.py
  7. 74 0
      gctools/portal.py
  8. 106 0
      gctools/publish-reports.mac
  9. 二進制
      gctools/publish-reports.mcx
  10. 0 0
      gctools/transformer11.py
  11. 0 0
      gctools/transformer7.py
  12. 1 1
      mazda-upload.py
  13. 24 20
      refresh.py
  14. 3 0
      test.log

二進制
Excel/Dash_LH_Wien.xls


文件差異過大導致無法顯示
+ 1 - 265
Planung/V1.csv


+ 19 - 19
Planung/V1.json

@@ -270,7 +270,7 @@
     "accounts": [],
     "costcenter": "NA",
     "level": 3,
-    "drilldown": false,
+    "drilldown": true,
     "form": "3",
     "accountlevel": false,
     "absolute": true,
@@ -742,10 +742,10 @@
       0,
       30,
       15000,
-      -2,
+      -2.5,
       5562967,
       185,
-      441176.4705882353,
+      439024.39024390245,
       0,
       0,
       0,
@@ -769,10 +769,10 @@
         0,
         30,
         15000,
-        -2,
+        -2.5,
         5562967,
         185,
-        441176.4705882353,
+        439024.39024390245,
         0,
         0,
         0,
@@ -969,10 +969,10 @@
       0,
       50,
       35000,
-      3,
+      3.5,
       1589333,
       40,
-      1804123.7113402062,
+      1813471.5025906737,
       0,
       0,
       0,
@@ -996,10 +996,10 @@
         0,
         50,
         35000,
-        3,
+        3.5,
         1589333,
         40,
-        1804123.7113402062,
+        1813471.5025906737,
         0,
         0,
         0,
@@ -5545,12 +5545,12 @@
       0,
       0,
       0,
-      10,
+      12,
       15000,
       2.5,
       0,
       0,
-      153846.15384615384,
+      184615.38461538462,
       0,
       0,
       0,
@@ -5572,12 +5572,12 @@
         0,
         0,
         0,
-        10,
+        12,
         15000,
         2.5,
         0,
         0,
-        153846.15384615384,
+        184615.38461538462,
         0,
         0,
         0,
@@ -40802,7 +40802,7 @@
       0,
       0,
       0,
-      150000,
+      180000,
       0,
       0,
       0,
@@ -40829,7 +40829,7 @@
         0,
         0,
         0,
-        150000,
+        180000,
         0,
         0,
         0,
@@ -64525,7 +64525,7 @@
     "accounts": [],
     "costcenter": "",
     "level": 1,
-    "drilldown": false,
+    "drilldown": true,
     "form": "1",
     "accountlevel": false,
     "absolute": true,
@@ -64604,7 +64604,7 @@
     "accounts": [],
     "costcenter": "NA",
     "level": 2,
-    "drilldown": false,
+    "drilldown": true,
     "form": "1",
     "accountlevel": false,
     "absolute": true,
@@ -69815,7 +69815,7 @@
     "accounts": [],
     "costcenter": "",
     "level": 1,
-    "drilldown": false,
+    "drilldown": true,
     "form": "1",
     "accountlevel": false,
     "absolute": true,
@@ -69899,7 +69899,7 @@
     "accounts": [],
     "costcenter": "NA",
     "level": 2,
-    "drilldown": false,
+    "drilldown": true,
     "form": "1",
     "accountlevel": false,
     "absolute": true,

+ 2 - 2
file-export.py

@@ -17,7 +17,7 @@ def expand(df, header, values_label):
 def apply_season(df):
     df['Saison'] = df['Ebene1'].str.contains('Umsatzerlöse|Materialaufwand|Verkaufsabh. Kosten')
     for i, key in enumerate(season_header):
-        df[key] = np.where(df['Saison'], df[key] * df[key + '_2'] / 100, df[key] * 8.3333 / 100)
+        df[key] = np.where(df['Saison'], df['Plan'] * df[key + '_2'] / 100, df['Plan'] * 8.3333 / 100)
     return df
 
 
@@ -40,5 +40,5 @@ plan = df[df['accounts'].apply(lambda a: len(a) > 0)]
 plan = pd.merge(plan, season, how='left', on=['Ebene2', 'department'], suffixes=('', '_2'))
 plan = apply_season(plan)
 
-plan = plan[header]
+# plan = plan[header]
 plan.to_csv(open('Planung/V1.csv', 'w', newline=''), sep=';', decimal=',', encoding='ansi', index=False)

+ 7 - 0
gctools/config.py

@@ -0,0 +1,7 @@
+class config:
+    portal_dir = 'C:\\GAPS\\Portal'
+    tasks_dir = portal_dir + '\\Tasks'
+    xml_dir = tasks_dir + '\\config'
+    log_dir = tasks_dir + '\\logs'
+    cognos_dir = 'C:\\Program Files (x86)\\Cognos\\cer5\\bin'
+    tools_dir = 'C:\\Projekte\\Python\\gctools'

+ 4 - 0
gctools/logger.py

@@ -0,0 +1,4 @@
+from datetime import datetime
+
+with open("test.log", "a") as f:
+    f.write('Running at ' + datetime.now().isoformat() + '\n')

+ 74 - 0
gctools/portal.py

@@ -0,0 +1,74 @@
+import subprocess
+import time
+from datetime import datetime
+from pathlib import Path
+import config
+
+cfg = config.config()
+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'
+    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}')
+    if not pub_dir.exists():
+        pub_dir.mkdir()
+
+    with open(f'{cfg.log_dir}\\{portal_file}.mac.log', 'w') as logfile:
+        export_files('jpg', pub_dir, logfile)
+        export_files('xls', pub_dir, logfile)
+        export_files('pdf', pub_dir, logfile)
+
+
+def export_files(export_format, pub_dir, logfile):
+    rep_dir = f'{report_dir}\\{export_format}'
+    cmd = f'"{cfg.cognos_dir}\\runmac32.exe" "{cfg.tools_dir}\\publish-reports.mac" "{rep_dir}","{export_format}","{pub_dir}"'
+    print(f"Exportiere '{rep_dir}' nach '{pub_dir}'...")
+    start = datetime.now().timestamp()
+    p = subprocess.Popen(cmd, stdout=logfile, stderr=logfile)
+    reports = sorted(Path(rep_dir).glob('*.pp[rx]'))
+    exports = [translate_filename(f.name, export_format, str(pub_dir)) for f in reports]
+    exports_remaining = exports
+
+    while p.poll() is None:
+        exports_remaining = [f for f in exports_remaining if not f.exists() or f.stat().st_mtime < start]
+        print('Fortschritt: ' + str(len(exports) - len(exports_remaining)) + '/' + str(len(exports)) + '...', end='\r')
+        time.sleep(5)
+
+    print('Abgeschlossen.             ')
+
+    if export_format == 'jpg':
+        rename_files(pub_dir)
+        cleanup_dir(pub_dir)
+
+
+def translate_filename(filename, export_format, pub_dir):
+    if export_format == 'jpg':
+        return Path(pub_dir + '\\' + filename[:-4] + 'graph1.jpg')
+    return Path(pub_dir + '\\' + filename[:-4] + '_0.' + export_format)
+
+
+def rename_files(pub_dir):
+    for f in pub_dir.glob('*.jpg'):
+        if f.exists():
+            new_name = str(f).replace('graph', '_')
+            f.replace(Path(new_name))
+
+
+def cleanup_dir(pub_dir):
+    for f in pub_dir.glob('*.htm'):
+        f.unlink()
+
+
+def prepare_report_temp():
+    pass
+
+
+if __name__ == '__main__':
+    portal('GAPS_neu')

+ 106 - 0
gctools/publish-reports.mac

@@ -0,0 +1,106 @@
+Dim objApp As Object
+Dim objRep As Object
+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 exportFormat = "pdf"
+   rem publish = "C:\GAPS\Portal\daten"
+   
+   folder = GetField(Command, 1, ",")
+   exportFormat = GetField(Command, 2, ",")
+   publish = GetField(Command, 3, ",")
+
+   Dim filename as String
+   Dim exportFile as String
+   Dim objPDF as Object
+   
+   filename = Dir(folder + "\")
+
+   Do While filename <> ""
+      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
+
+
+      filename = Dir
+   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
+
+

二進制
gctools/publish-reports.mcx


+ 0 - 0
transformer11.py → gctools/transformer11.py


+ 0 - 0
transformer7.py → gctools/transformer7.py


+ 1 - 1
mazda-upload.py

@@ -107,7 +107,7 @@ def upload(data):
     s.auth = (user, password)
     s.headers.update(headers)
     # r = s.post(webservice + module, data=data)
-    r = s.get(authorization + '?client_id=globalcube&response_type=code')
+    r = s.get(authorization + '?response_type=code&client_id=globalcube&state=xyz&scope=write')
     print(r.text)
     r = s.post(token_url, data={'grant_type': 'authorization_code', 'code': ''})
     print(r.text)

+ 24 - 20
refresh.py

@@ -1,27 +1,31 @@
+import plac
 import win32com.client
 from pathlib import Path
 
-excel = win32com.client.Dispatch('Excel.Application')
-excel.DisplayAlerts = False
-# excel.Visible = True
 
-filename = Path('Excel\\Dash_LH_Wien.xls')
-path = str(filename.parent.resolve())
-print(filename.resolve())
+@plac.pos('filename', '', type=Path)
+def refresh(filename):
+    excel = win32com.client.Dispatch('Excel.Application')
+    excel.DisplayAlerts = False
 
-wb = excel.Workbooks.Open(filename.resolve())
+    print(f"Refresh '{filename.resolve()}'")
+    wb = excel.Workbooks.Open(filename.resolve())
 
-try:
-    for source in wb.LinkSources(1):
-        print(' --> ' + source)
-        if Path(source).exists():
-            excel.Workbooks.Open(source)
-        else:
-            print('     !! does not exist !!')
-except TypeError:
-    print(' --> ' + 'no LinkSources')
+    try:
+        for source in wb.LinkSources(1):
+            print(' --> ' + source)
+            if Path(source).exists():
+                excel.Workbooks.Open(source)
+            else:
+                print('     !! does not exist !!')
+    except TypeError:
+        print(' --> ' + 'no LinkSources')
 
-wb.RefreshAll()
-wb.Save()
-wb.Close(True)
-excel.Application.Quit()
+    wb.RefreshAll()
+    wb.Save()
+    wb.Close(True)
+    excel.Application.Quit()
+
+
+if __name__ == '__main__':
+    plac.call(refresh)

+ 3 - 0
test.log

@@ -0,0 +1,3 @@
+Running at 2020-11-21T18:08:45.159807
+Running at 2020-11-21T18:09:41.576807
+Running at 2020-11-21T18:10:17.686840

部分文件因文件數量過多而無法顯示