-
Notifications
You must be signed in to change notification settings - Fork 2
/
office.py
177 lines (136 loc) · 4.34 KB
/
office.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# -*- coding: utf-8 -*-
import enum
import sys
from win32com import client
from action import Action
class OfficeType(enum.Enum):
Word = 1
Excel = 2
PPT = 3
class OfficeApp:
App_Office_Word = "Word.Application"
App_Office_Excel = "Excel.Application"
App_Office_PPT = "Powerpoint.Application"
App_WPS_Word = "Kwps.Application"
App_WPS_Excel = "Ket.Application"
App_WPS_PPT = "Kwpp.Application"
class Office:
@staticmethod
def is_windows():
if sys.platform.find('win32') != -1:
return True
else:
return False
@staticmethod
def get_app_name(office_type):
if office_type == OfficeType.Word:
return OfficeApp.App_Office_Word, OfficeApp.App_WPS_Word
elif office_type == OfficeType.Excel:
return OfficeApp.App_Office_Excel, OfficeApp.App_WPS_Excel
elif office_type == OfficeType.PPT:
return OfficeApp.App_Office_PPT, OfficeApp.App_WPS_PPT
else:
return None
@staticmethod
def get_app(office_type):
app_name = Office.get_app_name(office_type)
if not app_name:
return
app = client.Dispatch(app_name[0])
if not app:
app = client.Dispatch(app_name[1])
return app
@staticmethod
def get_page_count(src_file, action):
if not Office.is_windows():
raise Exception('Unsupported platform!')
if action == Action.Word2Pdf:
office_type = OfficeType.Word
elif action == Action.Excel2Pdf:
office_type = OfficeType.Excel
elif action == Action.PPT2Pdf:
office_type = OfficeType.PPT
else:
return -1
app = Office.get_app(office_type)
if not app:
return -1
if office_type == OfficeType.Word:
return Office.get_word_count(src_file)
elif office_type == OfficeType.Excel:
return Office.get_excel_count(src_file)
elif office_type == OfficeType.PPT:
return Office.get_ppt_count(src_file)
else:
return -1
@staticmethod
def get_word_count(src_file):
if not Office.is_windows():
raise Exception('Unsupported platform!')
word = Office.get_app(OfficeType.Word)
if not word:
return -1
doc = word.Documents.Open(src_file)
doc.Repaginate()
count = doc.ComputeStatistics(2)
doc.Close()
word.Quit()
return count
@staticmethod
def get_excel_count(src_file):
if not Office.is_windows():
raise Exception('Unsupported platform!')
excel = Office.get_app(OfficeType.Excel)
if not excel:
return -1
doc = excel.Workbooks.Open(src_file)
count = doc.Sheets.Count
doc.Close()
excel.Quit()
return count
@staticmethod
def get_ppt_count(src_file):
if not Office.is_windows():
raise Exception('Unsupported platform!')
ppt = Office.get_app(OfficeType.PPT)
if not ppt:
return -1
doc = ppt.Presentations.Open(src_file)
count = len(doc.slides)
doc.Close()
ppt.Quit()
return count
@staticmethod
def word2pdf(src_file, dst_file):
if not Office.is_windows():
raise Exception('Unsupported platform!')
word = Office.get_app(OfficeType.Word)
if not word:
return
doc = word.Documents.Open(src_file)
doc.SaveAs(dst_file, FileFormat=17)
doc.Close()
word.Quit()
@staticmethod
def excel2pdf(src_file, dst_file):
if not Office.is_windows():
raise Exception('Unsupported platform!')
excel = Office.get_app(OfficeType.Excel)
if not excel:
return
doc = excel.Workbooks.Open(src_file)
#doc.SaveAs(dst_file, FileFormat=57)
doc.ExportAsFixedFormat(0, dst_file)
doc.Close()
excel.Quit()
@staticmethod
def ppt2pdf(src_file, dst_file):
if not Office.is_windows():
raise Exception('Unsupported platform!')
ppt = Office.get_app(OfficeType.PPT)
if not ppt:
return
doc = ppt.Presentations.Open(src_file)
doc.SaveAs(dst_file, FileFormat=32)
doc.Close()
ppt.Quit()