xlwings-utils 25.0.5__py3-none-any.whl → 25.0.5.post0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of xlwings-utils might be problematic. Click here for more details.

@@ -679,7 +679,64 @@ class block:
679
679
  """
680
680
  return self.vlookup(s, row_from=row_from, row_to=row_to, column1=column1, column2=column2, default=default)
681
681
 
682
+ def decode_to_files(self):
683
+ """
684
+ decode the block with encoded file(s) to individual local files
685
+ """
686
+
687
+ row=1
688
+ while row<=self.number_of_rows:
689
+ if self[row,1] and self[row,1].startswith('<file=') and self[row,1].endswith('>'):
690
+ filename=self[row,1][6:-1]
691
+ collect=[]
692
+ row+=1
693
+ print(f"{filename=}")
694
+
695
+ while self[row,1]!='</file>':
696
+ print(f"{self[row,1]=}")
697
+
698
+ if self[row,1]:
699
+ collect.append(self[row,1])
700
+ row+=1
701
+ print(f"{collect=}")
702
+ decoded=base64.b64decode(''.join(collect))
703
+ open(filename,'wb').write(decoded)
704
+ row+=1
705
+
706
+ @classmethod
707
+ def encode_files(cls, *files):
708
+ """
709
+ make a block with the given files encoded
710
+
711
+ Parameters
712
+ ----------
713
+ files : file names (str)
714
+ files to be encoded
682
715
 
716
+ Returns
717
+ -------
718
+ block with encoded files : block
719
+ not minimized!
720
+ """
721
+
722
+ bl =cls(number_of_rows=50000, number_of_columns=1)
723
+
724
+ n = 5000 # block size
725
+ row = 1
726
+ for file in files:
727
+ bl[row, 1] = f"<file={file}>"
728
+ row += 1
729
+ b64 = base64.b64encode(open(file, "rb").read()).decode("utf-8")
730
+ while b64:
731
+ b64_n = b64[:n]
732
+ bl[row, 1] = b64_n
733
+ row += 1
734
+ b64 = b64[n:]
735
+ bl[row, 1] = f"</file>"
736
+ row+=1
737
+ return bl
738
+
739
+
683
740
  class Capture:
684
741
  """
685
742
  specifies how to capture stdout
@@ -785,71 +842,21 @@ class Capture:
785
842
  @include_print.setter
786
843
  def include_print(self, value):
787
844
  self._include_print = value
845
+
788
846
 
789
-
790
- def trigger_VBA(book):
791
- """
792
- triggers the macro on sheet VBA in book
793
-
794
- Parameters
795
- ----------
796
- book : xw.Book
797
- book to use
798
- """
799
-
800
- book.sheets["VBA"]["A1"].value = "=NOW()"
801
-
802
-
803
- def init_transfer_files(book):
804
- """
805
- initializes the file info on sheet VBA in book
806
-
807
- should be called prior to calling any transfer_file()
808
-
809
- Parameters
810
- ----------
811
- book : xw.Book
812
- book to use
813
- """
814
-
815
- global row1
816
- book.sheets["VBA"].range((10, 1), (1000000, 1)).clear()
817
- row1 = 10
818
-
819
-
820
- def transfer_file(book, file):
847
+ def trigger(sheet):
821
848
  """
822
- makes the local file to be encoded on sheet VBA in book
823
- to be triggered later
824
-
825
- init_transfer_files should be called prior to any transfer_file()
849
+ triggers the macro on sheet
826
850
 
827
851
  Parameters
828
852
  ----------
829
- book : xw.Book
830
- book to use
831
-
832
- file : string
833
- name of file to be encoded
853
+ sheet : sheet
854
+ sheet to use
855
+
834
856
  """
835
857
 
836
- global row1
837
- ws = book.sheets("VBA")
838
- ws_block = block(number_of_rows=50000, number_of_columns=1)
839
- n = 5000 # block size
840
- row = 1
841
- ws_block[row, 1] = f"<file={file}>"
842
- row += 1
843
- b64 = base64.b64encode(open(file, "rb").read()).decode("utf-8")
844
- while b64:
845
- b64_n = b64[:n]
846
- ws_block[row, 1] = b64_n
847
- row += 1
848
- b64 = b64[n:]
849
- ws_block[row, 1] = f"</file>"
850
- ws.range((row1, 1)).value = ws_block.minimized().value
851
- row1 += ws_block.highest_used_row_number
852
-
858
+ sheet["A1"].value = "=NOW()"
853
859
 
860
+
854
861
  if __name__ == "__main__":
855
862
  ...
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xlwings_utils
3
- Version: 25.0.5
3
+ Version: 25.0.5.post0
4
4
  Summary: xlwings_utils
5
5
  Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/salabim/xlwings_utils
@@ -263,10 +263,93 @@ The capture buffer can also be retrieved as a string with `capture.str` and `cap
263
263
 
264
264
  Clearing the captured stdout buffer can be done at any time with `capture.clear()`.
265
265
 
266
+ ## Functionality for accessing local files via VBA
267
+
268
+ Currently, *xlwings Lite* does not provide access to the local file system. Therefore, xlwings_utils offers some functionality to trigger a VBA script as well as functionality to encode a file in the pyodide file system to a VBA sheet and to trigger writing the encoded file(s) to the local file system.
269
+
270
+ Note that the sheet must have a worksheet named `VBA`, and the VBA code will normally reside there.
271
+
272
+ There are three Python functions defined:
273
+
274
+ * `trigger_VBA()`
275
+ fires a VBA script, provided a VBA function is defined on the sheet, like:
276
+
277
+ ```
278
+ Sub Worksheet_Calculate()
279
+ If Me.Range("A1").Formula = "=NOW()" Then
280
+ Me.Range("A1").Value = Null
281
+ REM Code to run
282
+ End If
283
+ End Sub
284
+ ```
285
+ * `init_transfer_files()`
286
+ This should be called prior to any `transfer_file` call. It just removes all encoded files from the VBA sheet (if any)
287
+
288
+ * `transfer_file()`
289
+ Can be used to encode a file on the pyodide file system to the VBA sheet. Multiple files are allowed. The file name will be encoded on the sheet as well.
290
+
291
+ The VBA code below can be used to decode encoded file(s) and write to the local file system.
292
+
293
+ ```
294
+ Sub WriteFiles()
295
+
296
+ Dim vArr() As Byte
297
+ Dim S As String
298
+ Dim Column As Integer
299
+ Dim Row As Integer
300
+ Dim ws As Worksheet
301
+ Dim Count As Integer
302
+ Dim FileNames As String
303
+
304
+ Column = 1
305
+ Row = 1
306
+
307
+ ThisDir = ThisWorkbook.Path
308
+ Set ws = Me
309
+ Count = 0
310
+ While Row < 30000
311
+ Line = ws.Cells(Row, Column)
312
+ If InStr(Line, "<file=") = 1 And Right(Line, 1) = ">" Then
313
+ If Count <> 0 Then
314
+ FileNames = FileNames & ", "
315
+ End If
316
+ Count = Count + 1
317
+ FileNameOnly = Mid(Line, 7, Len(Line) - 7)
318
+ Filename = ThisDir & "/" & FileNameOnly
319
+ FileNames = FileNames & FileNameOnly
320
+
321
+ Row = Row + 1
322
+ S = ""
323
+ While ws.Cells(Row, Column) <> "</file>"
324
+ S = S & ws.Cells(Row, Column)
325
+ Row = Row + 1
326
+ Wend
327
+
328
+ vArr = Base64ToArray(S)
329
+
330
+ Open Filename For Binary Access Write As #1
331
+ WritePos = 1
332
+ Put #1, WritePos, vArr
333
+ Close #1
334
+ End If
335
+
336
+ Row = Row + 1
337
+
338
+ Wend
339
+ If Cells(4, 2) = "y" Then
340
+ ws.Range("A10:A1000000").Clear
341
+ End If
342
+
343
+ If Count = 0 Then
344
+ MsgBox "No files written"
345
+ Else
346
+ MsgBox "Successfully written " & Str(Count) & " file(s): " & FileNames
347
+ End If
348
+ ```
266
349
 
267
350
  ## Contact info
268
351
 
269
- You can contact Ruud van der Ham, the core developer, via ruud@salabim.org .
352
+ You can contact Ruud van der Ham, the core developer, at ruud@salabim.org.
270
353
 
271
354
  ## Badges
272
355
 
@@ -0,0 +1,6 @@
1
+ xlwings_utils/__init__.py,sha256=FdaRztevSu5akGL7KBUBRzqwLMRTdvVUuS2Kfp2f1Uc,68
2
+ xlwings_utils/xlwings_utils.py,sha256=KP4RPmEyajLF2pn9IS3QEQyYC_xBn1yNoWzdh3cqqSo,25238
3
+ xlwings_utils-25.0.5.post0.dist-info/METADATA,sha256=1amSYUkgWF76__slfIRyOpR-J-VbHjeoqmZxipWK21Q,13036
4
+ xlwings_utils-25.0.5.post0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
5
+ xlwings_utils-25.0.5.post0.dist-info/top_level.txt,sha256=kf5SEv0gZiRObPhUoYcc1O_iX_wwTOPeUIYvzyYeAM4,14
6
+ xlwings_utils-25.0.5.post0.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- xlwings_utils/__init__.py,sha256=FdaRztevSu5akGL7KBUBRzqwLMRTdvVUuS2Kfp2f1Uc,68
2
- xlwings_utils/xlwings_utils.py,sha256=LT2NxlvS0T9wY4Qi9UiINpS1S1nmM2IutoBgkWk3JDo,24760
3
- xlwings_utils-25.0.5.dist-info/METADATA,sha256=MBHmRPFi-ytcOioOS2oII7pWN9e6SyOXHV_M3CVSJBs,10317
4
- xlwings_utils-25.0.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
5
- xlwings_utils-25.0.5.dist-info/top_level.txt,sha256=kf5SEv0gZiRObPhUoYcc1O_iX_wwTOPeUIYvzyYeAM4,14
6
- xlwings_utils-25.0.5.dist-info/RECORD,,