-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
book.c
88 lines (72 loc) · 1.82 KB
/
book.c
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
#include <Python.h>
#include "xlsxwriter.h"
#include "book.h"
#include "sheet.h"
extern PyTypeObject SheetType;
static int
init(PyXLSXBook *self)
{
self->handler = NULL;
return 0;
}
static PyObject *
open_book(PyXLSXBook *self, PyObject *args)
{
const char* fileName;
if(!PyArg_ParseTuple(args, "s", &fileName))
return NULL;
self->handler = workbook_new(fileName);
Py_RETURN_TRUE;
}
static PyObject *
add_worksheet(PyXLSXBook *self, PyObject *args)
{
const char* name;
if(!PyArg_ParseTuple(args, "|s", &name))
return NULL;
if(!self->handler){
PyErr_SetString(PyExc_RuntimeError, "No file name specified");
return NULL;
}
lxw_worksheet *sheet = workbook_add_worksheet(self->handler, name);
if (!sheet) Py_RETURN_NONE;
PyXLSXSheet *obj = PyObject_New(PyXLSXSheet, &SheetType);
obj->handler = sheet;
obj->book = self->handler;
return (PyObject *)obj;
}
static void
dealloc(PyXLSXBook *self)
{
PyObject_Free(self);
}
static PyObject *
close_book(PyXLSXBook *self)
{
workbook_close(self->handler);
Py_RETURN_TRUE;
}
static PyMethodDef methods[] = {
{"open", (PyCFunction) open_book, METH_VARARGS,
"Open a file for writing"
"Returns False if error occurs."},
{"add_worksheet", (PyCFunction) add_worksheet, METH_VARARGS,
"Add a sheet into the book"
"Returns False if error occurs."},
{"close", (PyCFunction) close_book, METH_VARARGS,
"Close the file."
"Returns False if error occurs."},
{NULL}
};
PyTypeObject BookType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "book",
.tp_doc = "book definition",
.tp_basicsize = sizeof(PyXLSXBook),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_new = PyType_GenericNew,
.tp_init = (initproc) init,
.tp_dealloc = (destructor) dealloc,
.tp_methods = methods,
};