officeparser 2.2.2 → 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.
Files changed (3) hide show
  1. package/README.md +201 -50
  2. package/officeParser.js +70 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -12,6 +12,7 @@ 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
15
16
  * 2020/06/01 - Added error handling and console.log enable/disable methods. Default is set at enabled. Everything backward compatible.
16
17
  * 2019/06/17 - Added method to change location for decompressing office files in places with restricted write access.
17
18
  * 2019/04/30 - Removed case sensitive file extension bug. File names with capital lettered extensions now supported.
@@ -34,71 +35,125 @@ npm i officeparser
34
35
  ----------
35
36
 
36
37
  **Usage**
37
- ```
38
+ ```js
38
39
  const officeParser = require('officeparser');
39
40
 
41
+ // callback
40
42
  officeParser.parseOffice("/path/to/officeFile", function(data, err){
41
- // "data" string in the callback here is the text parsed from the office file passed in the first argument above
42
- if (err) return console.log(err);
43
- console.log(data)
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)
44
46
  })
45
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
+ }
46
67
  ```
47
68
 
48
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.**
49
70
 
50
71
  *Optionally change decompression location for office Files at personalised locations for environments with restricted write access*
51
72
 
52
- ```
73
+ ```js
53
74
  const officeParser = require('officeparser');
54
75
 
55
76
  // Default decompress location for office Files is "officeDist" in the directory where Node is started.
56
77
  // Put this file before parseOffice method to take effect.
57
78
  officeParser.setDecompressionLocation("/tmp"); // New decompression location would be "/tmp/officeDist"
58
79
 
59
- // 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 '\\'
60
81
  officeParser.setDecompressionLocation("C:\\tmp"); // New decompression location would be "C:\tmp\officeDist"
61
82
 
62
83
 
63
84
  officeParser.parseOffice("/path/to/officeFile", function(data, err){
64
- // "data" string in the callback here is the text parsed from the office file passed in the first argument above
65
- if (err) return console.log(err);
66
- console.log(data)
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)
67
88
  })
68
89
  ```
69
90
 
70
91
  *Optionally add false as 3rd variable to parseOffice to not delete the generated officeDist folder*
71
92
 
72
- ```
93
+ ```js
94
+ // callback
73
95
  officeParser.parseOffice("/path/to/officeFile", function(data, err){
74
- // "data" string in the callback here is the text parsed from the office file passed in the first argument above
75
- if (err) return console.log(err);
76
- console.log(data)
96
+ if (err) return console.log(err);
97
+ console.log(data)
77
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
+ }
78
117
  ```
79
118
 
80
119
  **Example**
81
- ```
120
+ ```js
82
121
  const officeParser = require('officeparser');
83
122
 
123
+ // callback
84
124
  officeParser.parseOffice("C:\\files\\myText.docx", function(data, err){
85
- if (err) return console.log(err);
86
- var newText = data + "look, I can parse a word file"
87
- callSomeOtherFunction(newText);
125
+ if (err) return console.log(err);
126
+ var newText = data + "look, I can parse a word file"
127
+ callSomeOtherFunction(newText);
88
128
  })
89
129
 
90
- officeParser.parseOffice("/Users/harsh/Desktop/files/mySlides.pptx", function(data, err){
91
- if (err) return console.log(err);
92
- var newText = data + "look, I can parse a powerpoint file"
93
- 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);
94
136
  })
137
+ .catch(err) => {
138
+ console.log(err)
139
+ }
95
140
 
96
141
  // Using relative path for file is also fine
97
142
  officeParser.parseOffice("files/myWorkSheet.ods", function(data, err){
98
- if (err) return console.log(err);
99
- var newText = data + "look, I can parse an excel file"
100
- callSomeOtherFunction(newText);
143
+ if (err) return console.log(err);
144
+ var newText = data + "look, I can parse an excel file"
145
+ callSomeOtherFunction(newText);
101
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
+ }
102
157
  ```
103
158
 
104
159
 
@@ -108,62 +163,158 @@ officeParser.parseOffice("files/myWorkSheet.ods", function(data, err){
108
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.*
109
164
 
110
165
  **Usage**
111
- ```
166
+ ```js
112
167
  const officeParser = require('officeparser');
113
168
 
169
+ // callback
114
170
  officeParser.parseWord("/path/to/word.docx", function(data, err){
115
- // "data" string in the callback here is the text parsed from the word file passed in the first argument above
116
- if (err) return console.log(err);
117
- console.log(data)
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)
118
174
  })
119
175
 
120
176
  officeParser.parsePowerPoint("/path/to/powerpoint.pptx", function(data, err){
121
- // "data" string in the callback here is the text parsed from the powerpoint file passed in the first argument above
122
- if (err) return console.log(err);
123
- console.log(data)
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)
124
180
  })
125
181
 
126
182
  officeParser.parseExcel("/path/to/excel.xlsx", function(data, err){
127
- // "data" string in the callback here is the text parsed from the excel file passed in the first argument above
128
- if (err) return console.log(err);
129
- console.log(data)
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)
130
186
  })
131
187
 
132
188
  officeParser.parseOpenOffice("/path/to/writer.odt", function(data, err){
133
- // "data" string in the callback here is the text parsed from the writer file passed in the first argument above
134
- if (err) return console.log(err);
135
- console.log(data)
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)
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
136
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
+ }
137
226
  ```
138
227
 
139
228
  **Example**
140
- ```
229
+ ```js
141
230
  const officeParser = require('officeparser');
142
231
 
232
+ // callback
143
233
  officeParser.parseWord("C:\\files\\myText.docx", function(data, err){
144
- if (err) return console.log(err);
145
- var newText = data + "look, I can parse a word file"
146
- callSomeOtherFunction(newText);
234
+ if (err) return console.log(err);
235
+ var newText = data + "look, I can parse a word file"
236
+ callSomeOtherFunction(newText);
147
237
  })
148
238
 
149
239
  officeParser.parsePowerPoint("/Users/harsh/Desktop/files/mySlides.pptx", function(data, err){
150
- if (err) return console.log(err);
151
- var newText = data + "look, I can parse a powerpoint file"
152
- callSomeOtherFunction(newText);
240
+ if (err) return console.log(err);
241
+ var newText = data + "look, I can parse a powerpoint file"
242
+ callSomeOtherFunction(newText);
153
243
  })
154
244
 
155
245
  // Using relative path for file is also fine
156
246
  officeParser.parseExcel("files/myWorkSheet.xlsx", function(data, err){
157
- if (err) return console.log(err);
158
- var newText = data + "look, I can parse an excel file"
159
- callSomeOtherFunction(newText);
247
+ if (err) return console.log(err);
248
+ var newText = data + "look, I can parse an excel file"
249
+ callSomeOtherFunction(newText);
160
250
  })
161
251
 
162
252
  officeParser.parseOpenOffice("files/myDocument.odt", function(data, err){
163
- if (err) return console.log(err);
164
- var newText = data + "look, I can parse an OpenOffice file"
165
- callSomeOtherFunction(newText);
253
+ if (err) return console.log(err);
254
+ var newText = data + "look, I can parse an OpenOffice file"
255
+ callSomeOtherFunction(newText);
256
+ })
257
+
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);
166
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
+ }
167
318
  ```
168
319
 
169
320
  ----------
package/officeParser.js CHANGED
@@ -501,13 +501,83 @@ function disableConsoleOutput() {
501
501
 
502
502
  // #endregion setConsoleOutput
503
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
+ }
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
+ }
504
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
505
570
 
506
571
  module.exports.parseWord = parseWord;
507
572
  module.exports.parsePowerPoint = parsePowerPoint;
508
573
  module.exports.parseExcel = parseExcel;
509
574
  module.exports.parseOpenOffice = parseOpenOffice;
510
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;
511
581
  module.exports.setDecompressionLocation = setDecompressionLocation;
512
582
  module.exports.enableConsoleOutput = enableConsoleOutput;
513
583
  module.exports.disableConsoleOutput = disableConsoleOutput;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "officeparser",
3
- "version": "2.2.2",
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": {