Skip to content

Commit 8987efa

Browse files
Davi Kawasaki - DSVDavi Kawasaki - DSV
Davi Kawasaki - DSV
authored and
Davi Kawasaki - DSV
committed
Forking project from bitbucket
1 parent 342356e commit 8987efa

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Python Excel Library
2+
3+
Python Excel classes and snippets used for inner coding developments.
4+
5+
## Table of Contents
6+
7+
- [Libraries Dependencies](#libraries-dependencies)
8+
- [Versions](#versions)
9+
- [Changelog](#changelog)
10+
- [Authors](#authors)
11+
12+
## Libraries Dependencies
13+
14+
- win32com
15+
- dsvxpressmisc
16+
17+
## Versions
18+
19+
- dsvxpressexcel
20+
- ExcelVBA: [15/10/2018] 0.2.5
21+
22+
## Changelog
23+
24+
- dsvxpressexcel
25+
- ExcelVBA
26+
- [31/08/2018] 0.1.0: Class bootstrap and initialization
27+
- [05/09/2018] 0.2.0: Remove existent modules + unicode title treatment + bug fixes
28+
- [19/09/2018] 0.2.1: Bug fix in macro removal iteration + open workbook config override options
29+
- [05/10/2018] 0.2.2: Updated pip requirements for pywin32 installation
30+
- [05/10/2018] 0.2.3: Fixes for installing inner dependencies
31+
- [05/10/2018] 0.2.4: Bug fix when naming modules with non-special characters
32+
- [15/10/2018] 0.2.5: Bug fix - looping components without iterator index to properly remove all modules
33+
34+
## Authors
35+
36+
- Davi Shinji Mota Kawasaki

__init__.py

Whitespace-only changes.

pythonexcel/ExcelVBA.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#! /usr/bin/env python
2+
# Version: 0.2.5
3+
4+
from win32com.client import Dispatch
5+
from pythonmisc import string_manipulation as sm
6+
7+
8+
class ExcelVBA(object):
9+
10+
visible = False
11+
display_alerts = False
12+
instance = None
13+
14+
def __init__(self, visible, alerts):
15+
self.instance = Dispatch("Excel.Application")
16+
self.instance.Visible = visible or self.visible
17+
self.instance.DisplayAlerts = alerts or self.display_alerts
18+
19+
def get_workbook_from_file(self, filepath, visible, alerts):
20+
wb = self.instance.Workbooks.Open(filepath)
21+
wb = Dispatch(wb)
22+
# Override options, since can be a new file without the configs from self.instance
23+
self.instance.Visible = visible
24+
self.instance.DisplayAlerts = alerts
25+
return wb
26+
27+
def save_workbook_in_file(self, wb, filepath):
28+
return wb.saveAs(filepath)
29+
30+
def add_module(self, wb, name, pattern_type, str_code):
31+
xlmodule = wb.VBProject.VBComponents.Add(1)
32+
module_name = (name + pattern_type).encode('ascii', 'ignore')
33+
# Modules names aren't accepted with special characters (only letters)
34+
module_name = sm.remove_special_characters(module_name)
35+
xlmodule.CodeModule.Name = module_name
36+
xlmodule.CodeModule.AddFromString(str_code)
37+
return xlmodule
38+
39+
def add_class(self, wb, name, str_code):
40+
xlclass = wb.VBProject.VBComponents.Add(2)
41+
module_name = name.encode('ascii', 'ignore')
42+
# Modules names aren't accepted with special characters (only letters)
43+
module_name = sm.remove_special_characters(module_name)
44+
xlclass.CodeModule.Name = module_name
45+
xlclass.CodeModule.AddFromString(str_code)
46+
return xlclass
47+
48+
def add_form(self, wb):
49+
xlform = wb.VBProject.VBComponents.Add(3)
50+
return xlform
51+
52+
def get_module(self, wb, name, pattern_type):
53+
try:
54+
full_name = (name + pattern_type).encode('ascii', 'ignore')
55+
# Modules names aren't accepted with special characters (only letters)
56+
full_name = sm.remove_special_characters(full_name)
57+
return wb.VBProject.VBComponents(full_name)
58+
except Exception as e:
59+
# print e.args[2]
60+
print 'Module does not exist!'
61+
return None
62+
63+
def import_component(self, wb, name, pattern_type, filepath):
64+
xlcomponent = wb.VBProject.VBComponents.Import(filepath)
65+
if pattern_type == 'class':
66+
module_name = name.encode('ascii', 'ignore')
67+
# Modules names aren't accepted with special characters (only letters)
68+
module_name = sm.remove_special_characters(module_name)
69+
xlcomponent.CodeModule.Name = module_name
70+
else:
71+
module_name = (name + pattern_type).encode('ascii', 'ignore')
72+
# Modules names aren't accepted with special characters (only letters)
73+
module_name = sm.remove_special_characters(module_name)
74+
xlcomponent.CodeModule.Name = module_name
75+
return xlcomponent
76+
77+
def export_component(self, wb, name, pattern_type, filepath):
78+
name = name.encode('ascii', 'ignore')
79+
pattern_type = pattern_type.encode('ascii', 'ignore')
80+
# Modules names aren't accepted with special characters (only letters)
81+
name = sm.remove_special_characters(name)
82+
xlcomponent = self.get_module(wb, name, pattern_type)
83+
if xlcomponent is not None:
84+
xlcomponent.Export(filepath)
85+
else:
86+
print 'Module does not exist and cannot be exported!'
87+
88+
def destroy(self):
89+
self.instance.Quit()
90+
91+
def remove_component(self, wb, name, pattern_type):
92+
name = name.encode('ascii', 'ignore')
93+
pattern_type = pattern_type.encode('ascii', 'ignore')
94+
# Modules names aren't accepted with special characters (only letters)
95+
name = sm.remove_special_characters(name)
96+
xlcomponent = self.get_module(wb, name, pattern_type)
97+
if xlcomponent is not None:
98+
xlcomponent.Remove()
99+
else:
100+
print 'Module does not exist and cannot be removed!'
101+
102+
def remove_all_components(self, wb):
103+
try:
104+
# for i in range(1, wb.VBProject.VBComponents.Count + 1):
105+
for component in wb.VBProject.VBComponents:
106+
# xlmodule = wb.VBProject.VBComponents(i)
107+
if component.Type in [1, 2, 3]:
108+
wb.VBProject.VBComponents.Remove(component)
109+
except Exception as e:
110+
print e

pythonexcel/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name = "pythonexcel"
2+
3+
import ExcelVBA

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-e git+https://github.com/davikawasaki/python-misc-module-library#egg=pythonmisc
2+
pypiwin32==223
3+
pywin32==224

setup.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import setuptools
2+
3+
4+
def read_dependencies(req_file):
5+
with open(req_file) as req:
6+
return [line.strip() for line in req]
7+
8+
9+
with open("README.md", "r") as fh:
10+
long_description = fh.read()
11+
12+
setuptools.setup(
13+
name="pythonexcel",
14+
version="0.2.5",
15+
author="Davi Kawasaki",
16+
author_email="davishinjik@gmail.com",
17+
description="Python Excel Modules",
18+
long_description=long_description,
19+
long_description_content_type="text/markdown",
20+
url="https://github.com/davikawasaki/python-excel-module-library",
21+
packages=setuptools.find_packages(),
22+
install_requires=read_dependencies("requirements.txt")
23+
)

0 commit comments

Comments
 (0)