officeparser 2.1.0 → 2.3.0

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.
@@ -0,0 +1,3 @@
1
+ {
2
+ "svn.ignoreMissingSvnWarning": true
3
+ }
package/README.md CHANGED
@@ -12,6 +12,8 @@ A Node.js library to parse text out of any office file.
12
12
 
13
13
 
14
14
  #### Update
15
+ * 2021/11/21 - Added promise way to existing callback functions
16
+ * 2020/06/01 - Added error handling and console.log enable/disable methods. Default is set at enabled. Everything backward compatible.
15
17
  * 2019/06/17 - Added method to change location for decompressing office files in places with restricted write access.
16
18
  * 2019/04/30 - Removed case sensitive file extension bug. File names with capital lettered extensions now supported.
17
19
  * 2019/04/23 - Added support for open office files *.odt, *.odp, *.ods through parseOffice function. Created a new method parseOpenOffice for those who prefer targetted functions.
@@ -33,52 +35,125 @@ npm i officeparser
33
35
  ----------
34
36
 
35
37
  **Usage**
38
+ ```js
39
+ const officeParser = require('officeparser');
40
+
41
+ // callback
42
+ officeParser.parseOffice("/path/to/officeFile", function(data, err){
43
+ // "data" string in the callback here is the text parsed from the office file passed in the first argument above
44
+ if (err) return console.log(err);
45
+ console.log(data)
46
+ })
47
+
48
+ // promise
49
+ officeParser.parseOfficeAsync("/path/to/officeFile");
50
+ // "data" string in the promise here is the text parsed from the office file passed in the argument above
51
+ .then((data) => {
52
+ console.log(data)
53
+ })
54
+ .catch(err) => {
55
+ console.log(err)
56
+ }
57
+
58
+ // async/await
59
+ try {
60
+ // "data" string returned from promise here is the text parsed from the office file passed in the argument
61
+ const data = await officeParser.parseOfficeAsync("/path/to/officeFile");
62
+ console.log(data);
63
+ } catch (err) {
64
+ // resolve error
65
+ console.log(err);
66
+ }
36
67
  ```
68
+
69
+ **Please take note: I have breached convention in placing err as second argument in my callback but please understand that I had to do it to not break other people's existing modules.**
70
+
71
+ *Optionally change decompression location for office Files at personalised locations for environments with restricted write access*
72
+
73
+ ```js
37
74
  const officeParser = require('officeparser');
38
75
 
39
76
  // Default decompress location for office Files is "officeDist" in the directory where Node is started.
40
77
  // Put this file before parseOffice method to take effect.
41
78
  officeParser.setDecompressionLocation("/tmp"); // New decompression location would be "/tmp/officeDist"
42
79
 
43
- // P.S.: Setting location on a Windows environment with '\' heirarchy requires to be entered twice '\\'
80
+ // P.S.: Setting location on a Windows environment with '\' hierarchy requires to be entered twice '\\'
44
81
  officeParser.setDecompressionLocation("C:\\tmp"); // New decompression location would be "C:\tmp\officeDist"
45
82
 
46
83
 
47
- officeParser.parseOffice("/path/to/officeFile", function(data){
48
- // "data" string in the callback here is the text parsed from the office file passed in the first argument above
49
- console.log(data)
84
+ officeParser.parseOffice("/path/to/officeFile", function(data, err){
85
+ // "data" string in the callback here is the text parsed from the office file passed in the first argument above
86
+ if (err) return console.log(err);
87
+ console.log(data)
50
88
  })
51
-
52
89
  ```
53
90
 
54
91
  *Optionally add false as 3rd variable to parseOffice to not delete the generated officeDist folder*
55
92
 
56
- ```
57
- officeParser.parseOffice("/path/to/officeFile", function(data){
58
- // "data" string in the callback here is the text parsed from the office file passed in the first argument above
59
- console.log(data)
93
+ ```js
94
+ // callback
95
+ officeParser.parseOffice("/path/to/officeFile", function(data, err){
96
+ if (err) return console.log(err);
97
+ console.log(data)
60
98
  }, false)
99
+
100
+ // promise
101
+ officeParser.parseOfficeAsync("/path/to/officeFile", false);
102
+ .then((data) => {
103
+ console.log(data)
104
+ })
105
+ .catch(err) => {
106
+ console.log(err)
107
+ }
108
+
109
+ // async/await
110
+ try {
111
+ const data = await officeParser.parseOfficeAsync("/path/to/officeFile", false);
112
+ console.log(data);
113
+ } catch (err) {
114
+ // resolve error
115
+ console.log(err);
116
+ }
61
117
  ```
62
118
 
63
119
  **Example**
64
- ```
120
+ ```js
65
121
  const officeParser = require('officeparser');
66
122
 
67
- officeParser.parseOffice("C:\\files\\myText.docx", function(data){
68
- var newText = data + "look, I can parse a word file"
69
- callSomeOtherFunction(newText);
123
+ // callback
124
+ officeParser.parseOffice("C:\\files\\myText.docx", function(data, err){
125
+ if (err) return console.log(err);
126
+ var newText = data + "look, I can parse a word file"
127
+ callSomeOtherFunction(newText);
70
128
  })
71
129
 
72
- officeParser.parseOffice("/Users/harsh/Desktop/files/mySlides.pptx", function(data){
73
- var newText = data + "look, I can parse a powerpoint file"
74
- callSomeOtherFunction(newText);
130
+
131
+ // promise
132
+ officeParser.parseOfficeAsync("/Users/harsh/Desktop/files/mySlides.pptx");
133
+ .then((data) => {
134
+ var newText = data + "look, I can parse a powerpoint file"
135
+ callSomeOtherFunction(newText);
75
136
  })
137
+ .catch(err) => {
138
+ console.log(err)
139
+ }
76
140
 
77
141
  // Using relative path for file is also fine
78
- officeParser.parseOffice("files/myWorkSheet.ods", function(data){
79
- var newText = data + "look, I can parse an excel file"
80
- callSomeOtherFunction(newText);
142
+ officeParser.parseOffice("files/myWorkSheet.ods", function(data, err){
143
+ if (err) return console.log(err);
144
+ var newText = data + "look, I can parse an excel file"
145
+ callSomeOtherFunction(newText);
81
146
  })
147
+
148
+ // async/await
149
+ try {
150
+ const data = await officeParser.parseOfficeAsync("/Users/harsh/Desktop/files/mySlides.pptx");
151
+ let newText = data + "look, I can parse a powerpoint file";
152
+ await callSomeOtherFunction(newText);
153
+ } catch (err) {
154
+ // resolve error
155
+ console.log(err);
156
+ }
82
157
  ```
83
158
 
84
159
 
@@ -88,54 +163,158 @@ officeParser.parseOffice("files/myWorkSheet.ods", function(data){
88
163
  *These were the initial methods of parsing text till parseOffice method came into existence. These still exist and form the skeleton to this module as parseOffice redirects the below functions anyway. These functions will forever remain available to guarantee long-term usage of this module. I will ensure backward-compatibility with all previous versions.*
89
164
 
90
165
  **Usage**
91
- ```
166
+ ```js
92
167
  const officeParser = require('officeparser');
93
168
 
94
- officeParser.parseWord("/path/to/word.docx", function(data){
95
- // "data" string in the callback here is the text parsed from the word file passed in the first argument above
96
- console.log(data)
169
+ // callback
170
+ officeParser.parseWord("/path/to/word.docx", function(data, err){
171
+ // "data" string in the callback here is the text parsed from the word file passed in the first argument above
172
+ if (err) return console.log(err);
173
+ console.log(data)
97
174
  })
98
175
 
99
- officeParser.parsePowerPoint("/path/to/powerpoint.pptx", function(data){
100
- // "data" string in the callback here is the text parsed from the powerpoint file passed in the first argument above
101
- console.log(data)
176
+ officeParser.parsePowerPoint("/path/to/powerpoint.pptx", function(data, err){
177
+ // "data" string in the callback here is the text parsed from the powerpoint file passed in the first argument above
178
+ if (err) return console.log(err);
179
+ console.log(data)
102
180
  })
103
181
 
104
- officeParser.parseExcel("/path/to/excel.xlsx", function(data){
105
- // "data" string in the callback here is the text parsed from the excel file passed in the first argument above
106
- console.log(data)
182
+ officeParser.parseExcel("/path/to/excel.xlsx", function(data, err){
183
+ // "data" string in the callback here is the text parsed from the excel file passed in the first argument above
184
+ if (err) return console.log(err);
185
+ console.log(data)
107
186
  })
108
187
 
109
- officeParser.parseOpenOffice("/path/to/writer.odt", function(data){
110
- // "data" string in the callback here is the text parsed from the writer file passed in the first argument above
111
- console.log(data)
188
+ officeParser.parseOpenOffice("/path/to/writer.odt", function(data, err){
189
+ // "data" string in the callback here is the text parsed from the writer file passed in the first argument above
190
+ if (err) return console.log(err);
191
+ console.log(data)
112
192
  })
193
+
194
+ // promise
195
+ officeParser.parseWordAsync("/path/to/word.docx");
196
+ .then((data) => {
197
+ // data is the parsed text
198
+ })
199
+ officeParser.parsePowerPointAsync("/path/to/powerpoint.pptx");
200
+ .then((data) => {
201
+ // data is the parsed text
202
+ })
203
+ officeParser.parseExcelAsync("/path/to/excel.xlsx");
204
+ .then((data) => {
205
+ // data is the parsed text
206
+ })
207
+ officeParser.parseOpenOfficeAsync("/path/to/writer.odt");
208
+ .then((data) => {
209
+ // data is the parsed text
210
+ })
211
+
212
+ // async/await
213
+ try {
214
+ // "data" string returned from promise here is the text parsed from the office file passed in the first argument
215
+ const data1 = await officeParser.parseWordAsync("/path/to/word.docx");
216
+
217
+ const data2 = await officeParser.parsePowerPointAsync("/path/to/powerpoint.pptx");
218
+
219
+ const data3 = await officeParser.parseExcelAsync("/path/to/excel.xlsx");
220
+
221
+ const data3 = await officeParser.parseOpenOfficeAsync("/path/to/writer.odt");
222
+ } catch (err) {
223
+ // resolve error
224
+ console.log(err);
225
+ }
113
226
  ```
114
227
 
115
228
  **Example**
116
- ```
229
+ ```js
117
230
  const officeParser = require('officeparser');
118
231
 
119
- officeParser.parseWord("C:\\files\\myText.docx", function(data){
120
- var newText = data + "look, I can parse a word file"
121
- callSomeOtherFunction(newText);
232
+ // callback
233
+ officeParser.parseWord("C:\\files\\myText.docx", function(data, err){
234
+ if (err) return console.log(err);
235
+ var newText = data + "look, I can parse a word file"
236
+ callSomeOtherFunction(newText);
122
237
  })
123
238
 
124
- officeParser.parsePowerPoint("/Users/harsh/Desktop/files/mySlides.pptx", function(data){
125
- var newText = data + "look, I can parse a powerpoint file"
126
- callSomeOtherFunction(newText);
239
+ officeParser.parsePowerPoint("/Users/harsh/Desktop/files/mySlides.pptx", function(data, err){
240
+ if (err) return console.log(err);
241
+ var newText = data + "look, I can parse a powerpoint file"
242
+ callSomeOtherFunction(newText);
127
243
  })
128
244
 
129
245
  // Using relative path for file is also fine
130
- officeParser.parseExcel("files/myWorkSheet.xlsx", function(data){
131
- var newText = data + "look, I can parse an excel file"
132
- callSomeOtherFunction(newText);
246
+ officeParser.parseExcel("files/myWorkSheet.xlsx", function(data, err){
247
+ if (err) return console.log(err);
248
+ var newText = data + "look, I can parse an excel file"
249
+ callSomeOtherFunction(newText);
250
+ })
251
+
252
+ officeParser.parseOpenOffice("files/myDocument.odt", function(data, err){
253
+ if (err) return console.log(err);
254
+ var newText = data + "look, I can parse an OpenOffice file"
255
+ callSomeOtherFunction(newText);
133
256
  })
134
257
 
135
- officeParser.parseOpenOffice("files/myDocument.odt", function(data){
136
- var newText = data + "look, I can parse an OpenOffice file"
137
- callSomeOtherFunction(newText);
258
+ // promise
259
+ officeParser.parseWordAsync("C:\\files\\myText.docx");
260
+ .then((data) => {
261
+ let newText1 = data1 + "look, I can parse a word file";
262
+ callSomeOtherFunction(newText1);
263
+ })
264
+ .catch(err) => {
265
+ console.log(err)
266
+ }
267
+
268
+ officeParser.parsePowerPointAsync("/Users/harsh/Desktop/files/mySlides.pptx");
269
+ .then((data) => {
270
+ let newText2 = data2 + "look, I can parse a powerpoint file";
271
+ callSomeOtherFunction(newText2);
272
+ })
273
+ .catch(err) => {
274
+ console.log(err)
275
+ }
276
+
277
+ officeParser.parseExcelAsync("files/myWorkSheet.xlsx");
278
+ .then((data) => {
279
+ let newText3 = data3 + "look, I can parse an excel file";
280
+ callSomeOtherFunction(newText3);
281
+ })
282
+ .catch(err) => {
283
+ console.log(err)
284
+ }
285
+
286
+ officeParser.parseOpenOfficeAsync("files/myDocument.odt");
287
+ .then((data) => {
288
+ let newText4 = data4 + "look, I can parse an OpenOffice file";
289
+ callSomeOtherFunction(newText4);
138
290
  })
291
+ .catch(err) => {
292
+ console.log(err)
293
+ }
294
+
295
+
296
+ // async/await
297
+ try {
298
+ const data1 = await officeParser.parseWordAsync("C:\\files\\myText.docx");
299
+ let newText1 = data1 + "look, I can parse a word file";
300
+ await callSomeOtherFunction(newText1);
301
+
302
+ const data2 = await officeParser.parsePowerPointAsync("/Users/harsh/Desktop/files/mySlides.pptx");
303
+ let newText2 = data2 + "look, I can parse a powerpoint file";
304
+ await callSomeOtherFunction(newText2);
305
+
306
+ // Using relative path for file is also fine
307
+ const data3 = await officeParser.parseExcelAsync("files/myWorkSheet.xlsx");
308
+ let newText3 = data3 + "look, I can parse an excel file";
309
+ await callSomeOtherFunction(newText3);
310
+
311
+ const data4 = await officeParser.parseOpenOfficeAsync("files/myDocument.odt");
312
+ let newText4 = data4 + "look, I can parse an OpenOffice file";
313
+ await callSomeOtherFunction(newText4);
314
+ } catch (err) {
315
+ // resolve error
316
+ console.log(err);
317
+ }
139
318
  ```
140
319
 
141
320
  ----------
package/officeParser.js CHANGED
@@ -8,6 +8,7 @@ const rimraf = require('rimraf');
8
8
 
9
9
  var myTextWord = [];
10
10
  var decompressLocation = "officeDist";
11
+ var outputToConsoleWhenRequired = true; // Default is set to true to let the user identify source of an error in node terminal
11
12
 
12
13
  async function scanForTextWord(result) {
13
14
  if (Array.isArray(result)) {
@@ -21,50 +22,59 @@ async function scanForTextWord(result) {
21
22
  }
22
23
  return;
23
24
  }
24
- else if(typeof(result) == "object") {
25
+ else if (typeof(result) == "object") {
25
26
  for (var property in result) {
26
27
  if (result.hasOwnProperty(property)) {
27
- if (typeof(result[property]) == "string"){
28
- if ((property == "w:t" || property == "_" ) && result[property] != "") {
28
+ if (typeof(result[property]) == "string") {
29
+ if ((property == "w:t" || property == "_") && result[property] != "") {
29
30
  await myTextWord.push(result[property]);
30
31
  }
31
32
  }
32
33
  else {
33
34
  await scanForTextWord(result[property]);
34
35
  }
35
-
36
36
  }
37
37
  }
38
38
  return;
39
39
  }
40
40
  }
41
41
 
42
- var parseWord = async function(filename, callback, deleteOfficeDist = true) {
42
+ var parseWord = async function (filename, callback, deleteOfficeDist = true) {
43
43
  if (validateFileExtension(filename, ["docx"])) {
44
- decompress(filename, decompressLocation).then(files => {
45
- myTextWord = [];
46
- if (fs.existsSync(decompressLocation + "/word/document.xml")) {
47
- fs.readFile(decompressLocation + '/word/document.xml', 'utf8', function (err,data) {
48
- if (err) {
49
- return console.log(err);
50
- }
51
-
52
- xml2js.parseString(data, async function (err, result) {
53
- await scanForTextWord(result);
54
-
55
- callback(myTextWord.join(" "));
56
- if (deleteOfficeDist == true) {
57
- rimraf(decompressLocation, function () {});
44
+ try {
45
+ decompress(filename, decompressLocation).then(files => {
46
+ myTextWord = [];
47
+ if (fs.existsSync(decompressLocation + "/word/document.xml")) {
48
+ fs.readFile(decompressLocation + '/word/document.xml', 'utf8', function (err,data) {
49
+ if (err) {
50
+ if (outputToConsoleWhenRequired) console.log(err);
51
+ return callback(undefined, err);
58
52
  }
59
- });
60
- });
61
- }
62
- else {
63
- if (deleteOfficeDist == true) {
64
- rimraf(decompressLocation, function () {});
53
+ xml2js.parseString(data, async function (err, result) {
54
+ await scanForTextWord(result);
55
+
56
+ callback(myTextWord.join(" "), undefined);
57
+ if (deleteOfficeDist == true) {
58
+ rimraf(decompressLocation, function () {});
59
+ }
60
+ });
61
+ });
65
62
  }
66
- }
67
- });
63
+ else {
64
+ if (deleteOfficeDist == true) {
65
+ rimraf(decompressLocation, function () {});
66
+ }
67
+ }
68
+ })
69
+ .catch(function (err) {
70
+ if (outputToConsoleWhenRequired) console.log(err);
71
+ return callback(undefined, err);
72
+ });
73
+ }
74
+ catch(err) {
75
+ if (outputToConsoleWhenRequired) console.log(err);
76
+ return callback(undefined, err);
77
+ }
68
78
  }
69
79
  }
70
80
 
@@ -88,15 +98,15 @@ async function scanForTextPowerPoint(result) {
88
98
  }
89
99
  return;
90
100
  }
91
- else if(typeof(result) == "object") {
101
+ else if (typeof(result) == "object") {
92
102
  for (var property in result) {
93
103
  if (result.hasOwnProperty(property)) {
94
- if (typeof(result[property]) == "string"){
104
+ if (typeof(result[property]) == "string") {
95
105
  if ((property == "a:t" || property == "_") && result[property] != "") {
96
106
  await myTextPowerPoint.push(result[property]);
97
107
  }
98
108
  }
99
- else if (typeof(result[property][0]) == "string"){
109
+ else if (typeof(result[property][0]) == "string") {
100
110
  if ((property == "a:t" || property == "_") && result[property] != "") {
101
111
  await myTextPowerPoint.push(result[property]);
102
112
  }
@@ -110,47 +120,55 @@ async function scanForTextPowerPoint(result) {
110
120
  }
111
121
  }
112
122
 
113
-
114
- var parsePowerPoint = function(filename, callback, deleteOfficeDist = true) {
123
+ var parsePowerPoint = function (filename, callback, deleteOfficeDist = true) {
115
124
  if (validateFileExtension(filename, ["pptx"])) {
116
- decompress(filename, decompressLocation).then(async files => {
117
- myTextPowerPoint = [];
118
-
119
- if (fs.existsSync(decompressLocation + '/ppt/slides')) {
120
- var slidesNum = await fs.readdirSync(decompressLocation + '/ppt/slides').length;
121
-
122
-
123
- for (var i=0; i<slidesNum-1; i++) {
124
- var parser = new xml2js.Parser();
125
-
126
- var myData = await util.promisify(fs.readFile)(`${decompressLocation}/ppt/slides/slide${i+1}.xml`, 'utf8');
127
- var result = await util.promisify(parser.parseString.bind(parser))(myData);
128
- await scanForTextPowerPoint(result);
129
- }
130
-
131
- if (fs.existsSync(decompressLocation + '/ppt/notesSlides')) {
132
- var notesSlidesNum = await fs.readdirSync(decompressLocation + '/ppt/notesSlides').length;
133
- for (var i=0; i<notesSlidesNum-1; i++) {
125
+ try {
126
+ decompress(filename, decompressLocation).then(async files => {
127
+ myTextPowerPoint = [];
128
+
129
+ if (fs.existsSync(decompressLocation + '/ppt/slides')) {
130
+ var slidesNum = await fs.readdirSync(decompressLocation + '/ppt/slides').length;
131
+
132
+ for (var i = 0; i < slidesNum - 1; i++) {
134
133
  var parser = new xml2js.Parser();
135
134
 
136
- var myData = await util.promisify(fs.readFile)(`${decompressLocation}/ppt/notesSlides/notesSlide${i+1}.xml`, 'utf8');
135
+ var myData = await util.promisify(fs.readFile)(`${decompressLocation}/ppt/slides/slide${i + 1}.xml`, 'utf8');
137
136
  var result = await util.promisify(parser.parseString.bind(parser))(myData);
138
137
  await scanForTextPowerPoint(result);
139
138
  }
140
- }
141
-
142
- callback(myTextPowerPoint.join(" "));
139
+
140
+ if (fs.existsSync(decompressLocation + '/ppt/notesSlides')) {
141
+ var notesSlidesNum = await fs.readdirSync(decompressLocation + '/ppt/notesSlides').length;
142
+ for (var i = 0; i < notesSlidesNum - 1; i++) {
143
+ var parser = new xml2js.Parser();
144
+
145
+ var myData = await util.promisify(fs.readFile)(`${decompressLocation}/ppt/notesSlides/notesSlide${i + 1}.xml`, 'utf8');
146
+ var result = await util.promisify(parser.parseString.bind(parser))(myData);
147
+ await scanForTextPowerPoint(result);
148
+ }
149
+ }
143
150
 
144
- if (deleteOfficeDist == true) {
145
- rimraf(decompressLocation, function () {});
151
+ callback(myTextPowerPoint.join(" "), undefined);
152
+
153
+ if (deleteOfficeDist == true) {
154
+ rimraf(decompressLocation, function () {});
155
+ }
146
156
  }
147
- }
148
- else {
149
- if (deleteOfficeDist == true) {
150
- rimraf(decompressLocation, function () {});
157
+ else {
158
+ if (deleteOfficeDist == true) {
159
+ rimraf(decompressLocation, function () {});
160
+ }
151
161
  }
152
- }
153
- });
162
+ })
163
+ .catch(function (err) {
164
+ if (outputToConsoleWhenRequired) console.log(err);
165
+ return callback(undefined, err);
166
+ });
167
+ }
168
+ catch (err) {
169
+ if (outputToConsoleWhenRequired) console.log(err);
170
+ return callback(undefined, err);
171
+ }
154
172
  }
155
173
  }
156
174
 
@@ -173,15 +191,15 @@ async function scanForTextExcelDrawing(result) {
173
191
  }
174
192
  return;
175
193
  }
176
- else if(typeof(result) == "object") {
194
+ else if (typeof(result) == "object") {
177
195
  for (var property in result) {
178
196
  if (result.hasOwnProperty(property)) {
179
- if (typeof(result[property]) == "string"){
197
+ if (typeof(result[property]) == "string") {
180
198
  if ((property == "a:t" || property == "_") && result[property] != "") {
181
199
  await myTextExcel.push(result[property]);
182
200
  }
183
201
  }
184
- else if (typeof(result[property][0]) == "string"){
202
+ else if (typeof(result[property][0]) == "string") {
185
203
  if ((property == "a:t" || property == "_") && result[property] != "") {
186
204
  await myTextExcel.push(result[property]);
187
205
  }
@@ -211,12 +229,12 @@ async function scanForTextExcelSharedStrings(result) {
211
229
  else if(typeof(result) == "object") {
212
230
  for (var property in result) {
213
231
  if (result.hasOwnProperty(property)) {
214
- if (typeof(result[property]) == "string"){
232
+ if (typeof(result[property]) == "string") {
215
233
  if ((property == "t" || property == "_") && result[property] != "") {
216
234
  await myTextExcel.push(result[property]);
217
235
  }
218
236
  }
219
- else if (typeof(result[property][0]) == "string"){
237
+ else if (typeof(result[property][0]) == "string") {
220
238
  if ((property == "t" || property == "_") && result[property] != "") {
221
239
  await myTextExcel.push(result[property]);
222
240
  }
@@ -261,54 +279,64 @@ async function scanForTextExcelWorkSheet(result) {
261
279
  }
262
280
  }
263
281
 
264
- var parseExcel = function(filename, callback, deleteOfficeDist = true) {
282
+ var parseExcel = function (filename, callback, deleteOfficeDist = true) {
265
283
  if (validateFileExtension(filename, ["xlsx"])) {
266
- decompress(filename, decompressLocation).then(async files => {
267
- myTextExcel = [];
268
-
269
-
270
- if (fs.existsSync(decompressLocation + '/xl/worksheets')) {
271
- var workSheetsNum = await fs.readdirSync(decompressLocation + '/xl/worksheets').length;
272
- for (var i=0; i<workSheetsNum-1; i++) {
273
- var parser = new xml2js.Parser();
274
-
275
- var myData = await util.promisify(fs.readFile)(`${decompressLocation}/xl/worksheets/sheet${i+1}.xml`, 'utf8');
276
- var result = await util.promisify(parser.parseString.bind(parser))(myData);
277
- await scanForTextExcelWorkSheet(result);
278
- }
279
-
280
- if (fs.existsSync(decompressLocation + '/xl/sharedStrings.xml')) {
281
- var parser = new xml2js.Parser();
282
-
283
- var myData = await util.promisify(fs.readFile)(`${decompressLocation}/xl/sharedStrings.xml`, 'utf8');
284
- var result = await util.promisify(parser.parseString.bind(parser))(myData);
285
- await scanForTextExcelSharedStrings(result);
286
- }
287
-
288
- if (fs.existsSync(decompressLocation + '/xl/drawings')) {
289
- var drawingsNum = await fs.readdirSync(decompressLocation + '/xl/drawings').length;
290
-
291
- for (var i=0; i<drawingsNum; i++) {
284
+ try {
285
+ decompress(filename, decompressLocation).then(async files => {
286
+ myTextExcel = [];
287
+
288
+
289
+ if (fs.existsSync(decompressLocation + '/xl/worksheets')) {
290
+ var workSheetsNum = await fs.readdirSync(decompressLocation + '/xl/worksheets').length;
291
+ for (var i = 0; i < workSheetsNum - 1; i++) {
292
292
  var parser = new xml2js.Parser();
293
-
294
- var myData = await util.promisify(fs.readFile)(`${decompressLocation}/xl/drawings/drawing${i+1}.xml`, 'utf8');
293
+
294
+ var myData = await util.promisify(fs.readFile)(`${decompressLocation}/xl/worksheets/sheet${i + 1}.xml`, 'utf8');
295
295
  var result = await util.promisify(parser.parseString.bind(parser))(myData);
296
- await scanForTextExcelDrawing(result);
296
+ await scanForTextExcelWorkSheet(result);
297
+ }
298
+
299
+ if (fs.existsSync(decompressLocation + '/xl/sharedStrings.xml')) {
300
+ var parser = new xml2js.Parser();
301
+
302
+ var myData = await util.promisify(fs.readFile)(`${decompressLocation}/xl/sharedStrings.xml`, 'utf8');
303
+ var result = await util.promisify(parser.parseString.bind(parser))(myData);
304
+ await scanForTextExcelSharedStrings(result);
305
+ }
306
+
307
+ if (fs.existsSync(decompressLocation + '/xl/drawings')) {
308
+ var drawingsNum = await fs.readdirSync(decompressLocation + '/xl/drawings').length;
309
+
310
+ for (var i = 0; i < drawingsNum; i++) {
311
+ var parser = new xml2js.Parser();
312
+
313
+ var myData = await util.promisify(fs.readFile)(`${decompressLocation}/xl/drawings/drawing${i + 1}.xml`, 'utf8');
314
+ var result = await util.promisify(parser.parseString.bind(parser))(myData);
315
+ await scanForTextExcelDrawing(result);
316
+ }
317
+ }
318
+
319
+ callback(myTextExcel.join(" "), undefined);
320
+
321
+ if (deleteOfficeDist == true) {
322
+ rimraf(decompressLocation, function () {});
297
323
  }
298
324
  }
299
-
300
- callback(myTextExcel.join(" "));
301
-
302
- if (deleteOfficeDist == true) {
303
- rimraf(decompressLocation, function () {});
304
- }
305
- }
306
- else {
307
- if (deleteOfficeDist == true) {
308
- rimraf(decompressLocation, function () {});
325
+ else {
326
+ if (deleteOfficeDist == true) {
327
+ rimraf(decompressLocation, function () {});
328
+ }
309
329
  }
310
- }
311
- });
330
+ })
331
+ .catch(function (err) {
332
+ if (outputToConsoleWhenRequired) console.log(err);
333
+ return callback(undefined, err);
334
+ });
335
+ }
336
+ catch (err) {
337
+ if (outputToConsoleWhenRequired) console.log(err);
338
+ return callback(undefined, err);
339
+ }
312
340
  }
313
341
  }
314
342
 
@@ -332,10 +360,10 @@ async function scanForTextOpenOffice(result) {
332
360
  }
333
361
  return;
334
362
  }
335
- else if(typeof(result) == "object") {
363
+ else if (typeof(result) == "object") {
336
364
  for (var property in result) {
337
365
  if (result.hasOwnProperty(property)) {
338
- if (typeof(result[property]) == "string"){
366
+ if (typeof(result[property]) == "string") {
339
367
  if (result[property] != "") {
340
368
  await myTextOpenOffice.push(result[property]);
341
369
  }
@@ -350,32 +378,43 @@ async function scanForTextOpenOffice(result) {
350
378
  }
351
379
  }
352
380
 
353
- var parseOpenOffice = async function(filename, callback, deleteOfficeDist = true) {
381
+ var parseOpenOffice = async function (filename, callback, deleteOfficeDist = true) {
354
382
  if (validateFileExtension(filename, ["odt", "odp", "ods"])) {
355
- decompress(filename, decompressLocation).then(files => {
356
- myTextOpenOffice = [];
357
- if (fs.existsSync(decompressLocation + "/content.xml")) {
358
- fs.readFile(decompressLocation + '/content.xml', 'utf8', function (err,data) {
359
- if (err) {
360
- return console.log(err);
361
- }
362
-
363
- xml2js.parseString(data, {"ignoreAttrs": true}, async function (err, result) {
364
- await scanForTextOpenOffice(result);
365
-
366
- callback(myTextOpenOffice.join(" "));
367
- if (deleteOfficeDist == true) {
368
- rimraf(decompressLocation, function () {});
383
+ try {
384
+ decompress(filename, decompressLocation).then(files => {
385
+ myTextOpenOffice = [];
386
+ if (fs.existsSync(decompressLocation + "/content.xml")) {
387
+ fs.readFile(decompressLocation + '/content.xml', 'utf8', function (err,data) {
388
+ if (err) {
389
+ if (outputToConsoleWhenRequired) console.log(err);
390
+ return callback(undefined, err);
369
391
  }
370
- });
371
- });
372
- }
373
- else {
374
- if (deleteOfficeDist == true) {
375
- rimraf(decompressLocation, function () {});
392
+
393
+ xml2js.parseString(data, {"ignoreAttrs": true}, async function (err, result) {
394
+ await scanForTextOpenOffice(result);
395
+
396
+ callback(myTextOpenOffice.join(" "), undefined);
397
+ if (deleteOfficeDist == true) {
398
+ rimraf(decompressLocation, function () {});
399
+ }
400
+ });
401
+ });
376
402
  }
377
- }
378
- });
403
+ else {
404
+ if (deleteOfficeDist == true) {
405
+ rimraf(decompressLocation, function () {});
406
+ }
407
+ }
408
+ })
409
+ .catch(function (err) {
410
+ if (outputToConsoleWhenRequired) console.log(err);
411
+ return callback(undefined, err);
412
+ });
413
+ }
414
+ catch (err) {
415
+ if (outputToConsoleWhenRequired) console.log(err);
416
+ return callback(undefined, err);
417
+ }
379
418
  }
380
419
  }
381
420
 
@@ -386,13 +425,18 @@ var parseOpenOffice = async function(filename, callback, deleteOfficeDist = true
386
425
 
387
426
  // #region validate file names
388
427
 
428
+ /**
429
+ * Legacy method to check whether file functions called are indeed applicable with files provided
430
+ * @param {*} filename file path
431
+ * @param {*} extension extensions of supported files
432
+ */
389
433
  function validateFileExtension(filename, extension) {
390
434
  for (var extensionIterator = 0; extensionIterator < extension.length; extensionIterator++) {
391
435
  if (extension[extensionIterator] == filename.split(".").pop().toLowerCase()) {
392
436
  return true;
393
437
  }
394
438
  }
395
- console.log(`Sorry, we currently support docx, pptx, xlsx, odt, odp, ods files only. Make sure you pass appropriate file with the parsing functions.`);
439
+ console.log(`[OfficeParser]: Sorry, we currently support docx, pptx, xlsx, odt, odp, ods files only. Make sure you pass appropriate file with the parsing functions.`);
396
440
  return false;
397
441
  }
398
442
 
@@ -424,7 +468,9 @@ function parseOffice(filename, callback, deleteOfficeDist = true) {
424
468
  }, deleteOfficeDist);
425
469
  }
426
470
  else {
427
- console.log(`Sorry, we currently support docx, pptx, xlsx, odt, odp, ods files only. Create a ticket in Issues on github to add support for ${extension} files. Stay tuned for further updates.`);
471
+ var errMessage = `[OfficeParser]: Sorry, we currently support docx, pptx, xlsx, odt, odp, ods files only. Create a ticket in Issues on github to add support for ${extension} files. Stay tuned for further updates.`;
472
+ if (outputToConsoleWhenRequired) console.log(errMessage);
473
+ return callback(undefined, errMessage);
428
474
  }
429
475
 
430
476
  }
@@ -434,16 +480,104 @@ function parseOffice(filename, callback, deleteOfficeDist = true) {
434
480
 
435
481
  // #region setDecompressionLocation
436
482
  function setDecompressionLocation(newLocation) {
437
- decompressLocation = newLocation + "/officeDist";
483
+ if (newLocation != undefined) {
484
+ decompressLocation = newLocation + "/officeDist";
485
+ }
486
+ else {
487
+ decompressLocation = "officeDist";
488
+ }
438
489
  }
439
490
 
440
491
  // #endregion setDecompressionLocation
441
492
 
493
+ // #region setConsoleOutput
494
+ function enableConsoleOutput() {
495
+ outputToConsoleWhenRequired = true;
496
+ }
497
+
498
+ function disableConsoleOutput() {
499
+ outputToConsoleWhenRequired = false;
500
+ }
501
+
502
+ // #endregion setConsoleOutput
503
+
504
+ // #region Async Versions
505
+ var parseWordAsync = function (filename, deleteOfficeDist = true) {
506
+ return new Promise((resolve, reject) => {
507
+ try {
508
+ parseWord(filename, function (data, err) {
509
+ if (err) return reject(err);
510
+ return resolve(data);
511
+ },deleteOfficeDist);
512
+ } catch (error) {
513
+ return reject(error);
514
+ }
515
+ })
516
+ }
442
517
 
518
+ var parsePowerPointAsync = function (filename, deleteOfficeDist = true) {
519
+ return new Promise((resolve, reject) => {
520
+ try {
521
+ parsePowerPoint(filename, function (data, err) {
522
+ if (err) return reject(err);
523
+ return resolve(data);
524
+ },deleteOfficeDist);
525
+ } catch (error) {
526
+ return reject(error);
527
+ }
528
+ })
529
+ }
530
+
531
+ var parseExcelAsync = function (filename, deleteOfficeDist = true) {
532
+ return new Promise((resolve, reject) => {
533
+ try {
534
+ parseExcel(filename, function (data, err) {
535
+ if (err) return reject(err);
536
+ return resolve(data);
537
+ },deleteOfficeDist);
538
+ } catch (error) {
539
+ return reject(error);
540
+ }
541
+ })
542
+ }
543
+
544
+ var parseOpenOfficeAsync = function (filename, deleteOfficeDist = true) {
545
+ return new Promise((resolve, reject) => {
546
+ try {
547
+ parseOpenOffice(filename, function (data, err) {
548
+ if (err) return reject(err);
549
+ return resolve(data);
550
+ },deleteOfficeDist);
551
+ } catch (error) {
552
+ return reject(error);
553
+ }
554
+ })
555
+ }
556
+
557
+ var parseOfficeAsync = function (filename, deleteOfficeDist = true) {
558
+ return new Promise((resolve, reject) => {
559
+ try {
560
+ parseOffice(filename, function (data, err) {
561
+ if (err) return reject(err);
562
+ return resolve(data);
563
+ },deleteOfficeDist);
564
+ } catch (error) {
565
+ return reject(error);
566
+ }
567
+ })
568
+ }
569
+ // #endregion Async Versions
443
570
 
444
571
  module.exports.parseWord = parseWord;
445
572
  module.exports.parsePowerPoint = parsePowerPoint;
446
573
  module.exports.parseExcel = parseExcel;
447
574
  module.exports.parseOpenOffice = parseOpenOffice;
448
575
  module.exports.parseOffice = parseOffice;
576
+ module.exports.parseWordAsync = parseWordAsync;
577
+ module.exports.parsePowerPointAsync = parsePowerPointAsync;
578
+ module.exports.parseExcelAsync = parseExcelAsync;
579
+ module.exports.parseOpenOfficeAsync = parseOpenOfficeAsync;
580
+ module.exports.parseOfficeAsync = parseOfficeAsync;
449
581
  module.exports.setDecompressionLocation = setDecompressionLocation;
582
+ module.exports.enableConsoleOutput = enableConsoleOutput;
583
+ module.exports.disableConsoleOutput = disableConsoleOutput;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "officeparser",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "description": "A Node.js library to parse text out of any office file. Currently supports docx, pptx, xlsx, odt, odp and ods files.",
5
5
  "main": "officeParser.js",
6
6
  "scripts": {