LeenO computo metrico con LibreOffice  3.22.0
Il software libero per la gestione di computi metrici e contabilità lavori.
LeenoPdf.py
Vai alla documentazione di questo file.
1 import os
2 import LeenoUtils
3 import DocUtils
4 import SheetUtils
5 import Dialogs
6 import LeenoSettings
7 import LeenoConfig
8 
9 _EXPORTSETTINGSITEMS = (
10  'npElencoPrezzi',
11  'npComputoMetrico',
12  'npCostiManodopera',
13  'npQuadroEconomico',
14  'cbElencoPrezzi',
15  'cbComputoMetrico',
16  'cbCostiManodopera',
17  'cbQuadroEconomico',
18 )
19 
21  cfg = LeenoConfig.Config()
22  data = DocUtils.loadDataBlock(oDoc, 'ImpostazioniExport')
23  if data is None or len(data) == 0:
24  data = cfg.readBlock('ImpostazioniExport', True)
25  return data
26 
27 def storeExportSettings(oDoc, es):
28  cfg = LeenoConfig.Config()
29 
30  DocUtils.storeDataBlock(oDoc, 'ImpostazioniExport', es)
31  cfg.writeBlock('ImpostazioniExport', es, True)
32 
33 
34 def prepareCover(oDoc, nDoc, docSubst):
35  '''
36  prepare cover page, if there's one
37  copy to nDoc document and fill it's data
38  return true if we've got a cover, false otherwise
39  docSubst is a dictionary with additional variable replacements
40  mostly used for [PAGINE], [OGGETTO] and [NUMERO_DOCUMENTO]
41  which are document dependent data
42  '''
43  # load print settings and look for cover
44  data, covers = LeenoSettings.loadPrintSettings(oDoc)
45  fileCopertine = data.get('fileCopertine')
46  copertina = data.get('copertina')
47  if fileCopertine is None or copertina is None:
48  return False
49  if fileCopertine == '' or copertina == '':
50  return False
51 
52  cDoc = DocUtils.loadDocument(fileCopertine)
53  if cDoc is None:
54  return False
55  if not copertina in cDoc.Sheets:
56  cDoc.close(False)
57  del cDoc
58  return False
59 
60  # we need to copy page style too
61  sheet = cDoc.Sheets[copertina]
62  pageStyle = sheet.PageStyle
63  if pageStyle is not None and pageStyle != '':
64  print("PAGE HAS STYLE")
65  pageStyles = cDoc.StyleFamilies.getByName('PageStyles')
66  style = pageStyles.getByName(pageStyle)
67  SheetUtils.copyPageStyle(nDoc, style)
68 
69  # cover is OK, copy to new document
70  pos = nDoc.Sheets.Count
71  nDoc.Sheets.importSheet(cDoc, copertina, pos)
72 
73  # if page has a print area, copy it too...
74  nDoc.Sheets[pos].PageStyle = sheet.PageStyle
75  if len(sheet.PrintAreas) > 0:
76  print("PAGE HAS PRINT AREA")
77  nDoc.Sheets[pos].PrintAreas = sheet.PrintAreas
78 
79  # replaces all placeholders with settings ones
80  settings = LeenoSettings.loadPageReplacements(oDoc)
81  for key, val in docSubst.items():
82  settings[key] = val
83  SheetUtils.replaceText(nDoc.Sheets[pos], settings)
84 
85  # close cover document and return
86  cDoc.close(False)
87  del cDoc
88  return True
89 
90 def prepareHeaderFooter(oDoc, docSubst):
91 
92  res = {}
93 
94  # load print settings, we need header and footer data
95  printSettings, dummy = LeenoSettings.loadPrintSettings(oDoc)
96 
97  # load replacement templates
98  replDict = LeenoSettings.loadPageReplacements(oDoc)
99  for key, val in docSubst.items():
100  replDict[key] = val
101 
102  # replace placeholders
103  for psKey in ('intSx', 'intCenter', 'intDx', 'ppSx', 'ppCenter', 'ppDx'):
104  if psKey in printSettings:
105  psVal = printSettings[psKey]
106  for replKey, replVal in replDict.items():
107 
108  # pagination needs some extra steps
109  if replKey in ('[PAGINA]', '[PAGINE]'):
110  continue
111 
112  while replKey in psVal:
113  psVal = psVal.replace(replKey, replVal)
114  res[psKey] = psVal
115 
116  return res
117 
118 def PdfDialog():
119  # dimensione verticale dei checkbox == dimensione bottoni
120  #dummy, hItems = Dialogs.getButtonSize('', Icon="Icons-24x24/settings.png")
121  nWidth, hItems = Dialogs.getEditBox('aa')
122 
123  # dimensione dell'icona col PDF
124  imgW = Dialogs.getBigIconSize()[0] * 2
125 
126  return Dialogs.Dialog(Title='Esportazione documenti PDF', Horz=False, CanClose=True, Items=[
127  Dialogs.HSizer(Items=[
128  Dialogs.VSizer(Items=[
129  Dialogs.Spacer(),
130  Dialogs.ImageControl(Image='Icons-Big/pdf.png', MinWidth=imgW),
131  Dialogs.Spacer(),
132  ]),
133  Dialogs.VSizer(Items=[
134  Dialogs.FixedText(Text='Tavola'),
135  Dialogs.Spacer(),
136  Dialogs.Edit(Id='npElencoPrezzi', Align=1, FixedHeight=hItems, FixedWidth=nWidth),
137  Dialogs.Spacer(),
138  Dialogs.Edit(Id='npComputoMetrico', Align=1, FixedHeight=hItems, FixedWidth=nWidth),
139  Dialogs.Spacer(),
140  Dialogs.Edit(Id='npCostiManodopera', Align=1, FixedHeight=hItems, FixedWidth=nWidth),
141  Dialogs.Spacer(),
142  Dialogs.Edit(Id='npQuadroEconomico', Align=1, FixedHeight=hItems, FixedWidth=nWidth),
143  ]),
144  Dialogs.Spacer(),
145  Dialogs.VSizer(Items=[
146  Dialogs.FixedText(Text='Oggetto'),
147  Dialogs.Spacer(),
148  Dialogs.CheckBox(Id="cbElencoPrezzi", Label="Elenco prezzi", FixedHeight=hItems),
149  Dialogs.Spacer(),
150  Dialogs.CheckBox(Id="cbComputoMetrico", Label="Computo metrico", FixedHeight=hItems),
151  Dialogs.Spacer(),
152  Dialogs.CheckBox(Id="cbCostiManodopera", Label="Costi manodopera", FixedHeight=hItems),
153  Dialogs.Spacer(),
154  Dialogs.CheckBox(Id="cbQuadroEconomico", Label="Quadro economico", FixedHeight=hItems),
155  ]),
156  Dialogs.Spacer(),
157  ]),
158  Dialogs.Spacer(),
159  Dialogs.Spacer(),
160  Dialogs.FixedText(Text='Cartella di destinazione:'),
161  Dialogs.Spacer(),
162  Dialogs.PathControl(Id="pathEdit"),
163  Dialogs.Spacer(),
164  Dialogs.HSizer(Items=[
165  Dialogs.Spacer(),
166  Dialogs.Button(Label='Ok', MinWidth=Dialogs.MINBTNWIDTH, Icon='Icons-24x24/ok.png', RetVal=1),
167  Dialogs.Spacer(),
168  Dialogs.Button(Label='Annulla', MinWidth=Dialogs.MINBTNWIDTH, Icon='Icons-24x24/cancel.png', RetVal=-1),
170  ])
171  ])
172 
173 
174 def PdfElencoPrezzi(destFolder, nTavola):
175 
176  oDoc = LeenoUtils.getDocument()
177  ep = oDoc.Sheets.getByName('Elenco Prezzi')
178 
179  # lancia l'export
180  nDoc = str(nTavola)
181  baseName = ''
182  if nDoc != '' and nDoc is not None:
183  baseName = nDoc + '-'
184  destPath = os.path.join(destFolder, baseName + 'ElencoPrezzi.pdf')
185  print(f"Export to '{destPath}' file")
186  selection = [ep, ]
187  docSubst = {
188  '[OGGETTO]':'Elenco Prezzi',
189  '[NUMERO_DOCUMENTO]': str(nTavola),
190  }
191  headerFooter = prepareHeaderFooter(oDoc, docSubst)
192  nPages = len(ep.RowPageBreaks) - 1
193 
194  # ~nPages = LeenoUtils.countPdfPages(destPath)
195  docSubst['[PAGINE]'] = nPages
196  SheetUtils.pdfExport(oDoc, selection, destPath, headerFooter, lambda oDoc, nDoc: prepareCover(oDoc, nDoc, docSubst))
197 
198 
199 def PdfComputoMetrico(destFolder, nTavola):
200 
201  oDoc = LeenoUtils.getDocument()
202  ep = oDoc.Sheets.getByName('COMPUTO')
203 
204  # lancia l'export
205  nDoc = str(nTavola)
206  baseName = ''
207  if nDoc != '' and nDoc is not None:
208  baseName = nDoc + '-'
209  destPath = os.path.join(destFolder, baseName + 'ComputoMetrico.pdf')
210  # ~print(f"Export to '{destPath}' file")
211  selection = [ep, ]
212  docSubst = {
213  '[OGGETTO]':'Computo Metrico',
214  '[NUMERO_DOCUMENTO]': str(nTavola),
215  }
216  headerFooter = prepareHeaderFooter(oDoc, docSubst)
217 
218  nPages = len(ep.RowPageBreaks) - 1
219  # ~nPages = LeenoUtils.countPdfPages(destPath)
220  docSubst['[PAGINE]'] = nPages
221  SheetUtils.pdfExport(oDoc, selection, destPath, headerFooter, lambda oDoc, nDoc: prepareCover(oDoc, nDoc, docSubst))
222 
223 
224 def MENU_Pdf():
225 
226  oDoc = LeenoUtils.getDocument()
227  es = loadExportSettings(oDoc)
228 
229  dlg = PdfDialog()
230  dlg.setData(es)
231 
232  # se premuto "annulla" non fa nulla
233  if dlg.run() < 0:
234  return
235 
236  es = dlg.getData(_EXPORTSETTINGSITEMS)
237  storeExportSettings(oDoc, es)
238 
239  # estrae la path
240  destFolder = dlg['pathEdit'].getPath()
241  # ~destFolder = 'W:\\_dwg\\ULTIMUSFREE\\_SRC'
242 
243  # ~import LeenoDialogs as DLG
244  # ~DLG.chi(destFolder)
245  # ~return
246 
247  # controlla se selezionato elenco prezzi
248  if dlg['cbElencoPrezzi'].getState():
249  PdfElencoPrezzi(destFolder, es['npElencoPrezzi'])
250 
251  # controlla se selezionato computo metrico
252  if dlg['cbComputoMetrico'].getState():
253  PdfComputoMetrico(destFolder, es['npComputoMetrico'])
254 
SheetUtils.pdfExport
def pdfExport(oDoc, sheets, destPath, HeaderFooter=None, coverBuilder=None)
Definition: SheetUtils.py:224
DocUtils.loadDocument
def loadDocument(filePath, Hidden=True)
Definition: DocUtils.py:82
SheetUtils.replaceText
def replaceText(sheet, replaceDict)
Definition: SheetUtils.py:55
LeenoPdf.prepareHeaderFooter
def prepareHeaderFooter(oDoc, docSubst)
Definition: LeenoPdf.py:90
LeenoUtils.getDocument
def getDocument()
Definition: LeenoUtils.py:67
Dialogs.Spacer
Definition: Dialogs.py:616
Dialogs.PathControl
Definition: Dialogs.py:1256
LeenoPdf.PdfElencoPrezzi
def PdfElencoPrezzi(destFolder, nTavola)
Definition: LeenoPdf.py:174
Dialogs.ImageControl
Definition: Dialogs.py:1387
LeenoPdf.MENU_Pdf
def MENU_Pdf()
Definition: LeenoPdf.py:224
LeenoPdf.PdfComputoMetrico
def PdfComputoMetrico(destFolder, nTavola)
Definition: LeenoPdf.py:199
Dialogs.Edit
Definition: Dialogs.py:1065
LeenoSettings.loadPrintSettings
def loadPrintSettings(oDoc)
Definition: LeenoSettings.py:176
Dialogs.FixedText
Definition: Dialogs.py:989
DocUtils.storeDataBlock
def storeDataBlock(oDoc, baseName, data)
Definition: DocUtils.py:56
LeenoPdf.PdfDialog
def PdfDialog()
Definition: LeenoPdf.py:118
Dialogs.HSizer
Definition: Dialogs.py:706
LeenoSettings.loadPageReplacements
def loadPageReplacements(oDoc)
Definition: LeenoSettings.py:58
Dialogs.Dialog
Definition: Dialogs.py:2189
Dialogs.Button
Definition: Dialogs.py:1527
LeenoConfig.Config
Definition: LeenoConfig.py:26
Dialogs.VSizer
Definition: Dialogs.py:848
Dialogs.CheckBox
Definition: Dialogs.py:1605
LeenoPdf.prepareCover
def prepareCover(oDoc, nDoc, docSubst)
Definition: LeenoPdf.py:34
Dialogs.getBigIconSize
def getBigIconSize()
Definition: Dialogs.py:103
LeenoPdf.storeExportSettings
def storeExportSettings(oDoc, es)
Definition: LeenoPdf.py:27
Dialogs.getEditBox
def getEditBox(txt)
Definition: Dialogs.py:134
LeenoPdf.loadExportSettings
def loadExportSettings(oDoc)
Definition: LeenoPdf.py:20
SheetUtils.copyPageStyle
def copyPageStyle(nDoc, style)
Definition: SheetUtils.py:185
DocUtils.loadDataBlock
def loadDataBlock(oDoc, baseName)
Definition: DocUtils.py:65