本文整理汇总了Python中pyexcel.get_book函数的典型用法代码示例。如果您正苦于以下问题:Python get_book函数的具体用法?Python get_book怎么用?Python get_book使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_book函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_payload
def _get_payload(self, email_info):
payload = {}
for part in email_info.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
f_name = part.get_filename()
f_ext = self._settings['Content Type'].get(part.get_content_type(), None)
# See if its possible to get f_ext from .get_content_charset()
if f_name and f_ext:
if f_ext == 'xlsx':
try:
f_path = join(self.parent.src_doc_path, f_name)
if not isfile(f_path):
with open(f_path, mode='wb') as f:
f.write(part.get_payload(decode=True))
f.close()
payload[f_name] = get_book(
file_name=f_path
)
except AttributeError:
with NamedTemporaryFile(mode='w+b', suffix=f_ext) as f:
f.write(part.get_payload(decode=True))
f.seek(0)
payload[f_name] = get_book(
file_type=f_ext,
file_content=f.read()
)
if f_ext == 'wav':
with NamedTemporaryFile(mode='w+b', suffix=f_ext) as f: # change this back to delete=False for scribing
f.write(part.get_payload(decode=True))
f.seek(0)
payload[f_name] = f
return payload
示例2: test_issue_23
def test_issue_23():
if not IN_TRAVIS:
raise SkipTest()
url = (
"https://github.com/pyexcel/pyexcel-ods3/"
+ "raw/master/tests/fixtures/multilineods.ods"
)
pe.get_book(url=url)
示例3: test_issue_23
def test_issue_23():
if not IN_TRAVIS:
raise SkipTest()
pe.get_book(
url=(
"https://github.com/pyexcel/pyexcel-ods/"
+ "raw/master/tests/fixtures/white_space.ods"
)
)
示例4: test_simple_option
def test_simple_option():
runner = CliRunner()
file_fixture = os.path.join("tests", "fixtures", "transcode_simple.csv")
dir_fixture = os.path.join("tests", "fixtures", "file_dir")
glob_fixture = os.path.join("tests", "fixtures", "glob_dir", "*")
output = "test_simple_option.xls"
result = runner.invoke(merge, [file_fixture, dir_fixture, glob_fixture,
output])
eq_(result.exit_code, 0)
book = get_book(file_name=output)
expected = dedent("""
transcode_simple.csv:
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
merge_test.csv:
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
merge_test2.csv:
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+""").strip('\n')
eq_(str(book), expected)
os.unlink(output)
示例5: testBook
def testBook(self):
fp = open(self.testfile, "rb")
response = self.client.post('/polls/import/', data={"file": fp})
assert response.status_code == 200
response2 = self.client.get('/polls/export/book')
assert response2.status_code == 200
book = pe.get_book(file_type='xls', file_content=response2.content)
content = dedent("""
Sheet Name: question
+----+---------------------------+----------------------------------------------+----------+
| id | pub_date | question_text | slug |
+----+---------------------------+----------------------------------------------+----------+
| 1 | 2015-01-28T00:00:00+00:00 | What is your favourite programming language? | language |
+----+---------------------------+----------------------------------------------+----------+
| 2 | 2015-01-29T00:00:00+00:00 | What is your favourite IDE? | ide |
+----+---------------------------+----------------------------------------------+----------+
Sheet Name: choice
+---------------+----+-------------+-------+
| choice_text | id | question_id | votes |
+---------------+----+-------------+-------+
| Java | 1 | 1 | 0 |
+---------------+----+-------------+-------+
| C++ | 2 | 1 | 0 |
+---------------+----+-------------+-------+
| C | 3 | 1 | 0 |
+---------------+----+-------------+-------+
| Eclipse | 4 | 2 | 0 |
+---------------+----+-------------+-------+
| Visual Studio | 5 | 2 | 0 |
+---------------+----+-------------+-------+
| PyCharm | 6 | 2 | 0 |
+---------------+----+-------------+-------+
| IntelliJ | 7 | 2 | 0 |
+---------------+----+-------------+-------+""").strip('\n')
assert str(book) == content
示例6: split
def split(source_file_type, output_file_type,
output_dir, source, output_file_prefix):
"""
Split an excel file into one sheet per file
\b
SOURCE: a file name or '-'. '-' tells the command to use stdin
OUTPUT_FILE_PREFIX: the splitted file name is form as
prefix_sheetname_index.file_type
"""
params = {}
if source == '-':
params['file_content'] = get_input_content(source_file_type)
params['file_type'] = source_file_type
else:
params['file_name'] = source
book = get_book(**params)
sheet_count = 0
for sheet in book:
file_name = "%s_%s_%d.%s" % (output_file_prefix, sheet.name,
sheet_count, output_file_type)
if output_dir is not None:
file_name = os.path.join(output_dir, file_name)
sheet.save_as(file_name)
sheet_count += 1
示例7: test_get_book_from_book_dict
def test_get_book_from_book_dict(self):
content = OrderedDict()
content.update({"Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]})
content.update({"Sheet2": [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]]})
content.update({"Sheet3": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]})
book = pe.get_book(bookdict=content)
assert book.to_dict() == content
示例8: test_get_book_from_file_stream
def test_get_book_from_file_stream(self):
content = OrderedDict()
content.update({"Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]})
content.update({"Sheet2": [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]]})
content.update({"Sheet3": [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]})
io = pe.save_book_as(dest_file_type="xls", bookdict=content)
book2 = pe.get_book(file_stream=io, file_type="xls")
assert book2.to_dict() == content
示例9: test_get_book_auto_detect_int
def test_get_book_auto_detect_int(self):
book = pe.get_book(file_name=self.test_file, library="pyexcel-xls")
expected = dedent("""
pyexcel_sheet1:
+---+---+-----+
| 1 | 2 | 3.1 |
+---+---+-----+""").strip()
eq_(str(book), expected)
示例10: get_book
def get_book(self, **keywords):
"""Get a instance of :class:`Book` from the file
:param keywords: additional key words
:returns: A instance of :class:`Book`
"""
params = self.get_params(**keywords)
return pe.get_book(**params)
示例11: test_add_book1
def test_add_book1(self):
"""
test this scenario: book3 = book1 + book2
"""
b1 = pyexcel.get_book(file_name=self.testfile)
b2 = pyexcel.get_book(file_name=self.testfile2)
b3 = b1 + b2
content = pyexcel.utils.to_dict(b3)
sheet_names = content.keys()
assert len(sheet_names) == 6
for name in sheet_names:
if "Sheet3" in name:
assert content[name] == self.content["Sheet3"]
elif "Sheet2" in name:
assert content[name] == self.content["Sheet2"]
elif "Sheet1" in name:
assert content[name] == self.content["Sheet1"]
示例12: test_get_book_auto_detect_int_false
def test_get_book_auto_detect_int_false(self):
book = pe.get_book(file_name=self.test_file, auto_detect_int=False)
expected = dedent("""
test_auto_detect_init.csv:
+-----+-----+-----+
| 1.0 | 2.0 | 3.1 |
+-----+-----+-----+""").strip()
self.assertEqual(str(book), expected)
示例13: test_load_a_single_sheet
def test_load_a_single_sheet(self):
b1 = pyexcel.get_book(
file_name=self.testfile,
sheet_name="Sheet1",
library="pyexcel-xlsx",
)
assert len(b1.sheet_names()) == 1
assert b1["Sheet1"].to_array() == self.content["Sheet1"]
示例14: load_book
def load_book(self, field_name=None, **keywords):
file_type, file_handle = self.get_file_tuple(field_name)
if file_type is not None and file_handle is not None:
return pe.get_book(file_type=file_type,
file_content=file_handle.read(),
**keywords)
else:
return None
示例15: test_load_book_from_django_model
def test_load_book_from_django_model(self):
model=FakeDjangoModel("Sheet1")
book = pe.Book(self.content)
book.save_to_django_models([model])
assert model.objects.objs == self.result1
model._meta.update(["X", "Y", "Z"])
book2 = pe.get_book(models=[model])
assert book2[0].to_array() == book[0].to_array()