Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def do_read_stringio(file_name):
create_sample_file1(file_name)
file_type = file_name.split(".")[-1]
open_flag = "rb"
if file_type in ["csv", "tsv"]:
open_flag = "r"
with open(file_name, open_flag) as f:
content = f.read()
r = pe.get_sheet(file_type=file_type, file_content=content)
result = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 1.1, 1]
actual = list(r.enumerate())
eq_(result, actual)
if os.path.exists(file_name):
os.unlink(file_name)
def test_writing_multiline_ods():
content = "2\n3\n4\n993939\n\na"
testfile = "writemultiline.ods"
array = [[content, "test"]]
pyexcel.save_as(array=array, dest_file_name=testfile)
sheet = pyexcel.get_sheet(file_name=testfile)
assert sheet[0, 0] == content
os.unlink(testfile)
def test_write_array(self):
self._create_a_file(self.testfile)
r = pyexcel.get_sheet(file_name=self.testfile)
actual = list(r.rows())
assert actual == self.content
def test_save_as(self):
data = [
["birth", "name", "weight"],
[datetime.date(2017, 1, 11), "Adam", 3.0],
]
session = Session()
pe.save_as(array=data, dest_session=session, dest_table=Pyexcel)
sheet = pe.get_sheet(session=session, table=Pyexcel)
eq_(str(sheet), "")
def test_save_as_1(self):
data = [
["birth", "name", "weight"],
[datetime.date(2017, 1, 11), "Adam", 3.0],
]
session = Session()
pe.save_as(
array=data,
name_columns_by_row=0,
dest_session=session,
dest_table=Pyexcel,
)
sheet = pe.get_sheet(session=session, table=Pyexcel)
print(sheet)
content = dedent(
"""
pyexcel:
+------------+----+------+--------+
| birth | id | name | weight |
+------------+----+------+--------+
| 2017-01-11 | 1 | Adam | 3.0 |
+------------+----+------+--------+"""
).strip("\n")
eq_(str(sheet), content)
def verify(self):
sheet2 = pe.get_sheet(file_name=OUTPUT)
assert sheet2.to_array() == self.data
eq_(sheet2.name, self.test_sheet_name)
def _download_and_verify_file_name(self, file_name):
for file_type in FILE_TYPE_MIME_TABLE.keys():
url_encoded_file_name = urllib_quote(file_name)
response = self.client.get(
("/polls/download_attachment/%s/%s" % (
file_type, url_encoded_file_name)))
assert response['Content-Type'] == FILE_TYPE_MIME_TABLE[file_type]
assert response['Content-Disposition'] == (
"attachment; filename=%s.%s;filename*=utf-8''%s.%s"
% (url_encoded_file_name, file_type,
url_encoded_file_name, file_type))
sheet = pe.get_sheet(file_type=file_type,
file_content=response.content)
sheet.format(int)
array = sheet.to_array()
assert array == self.data
def test_issue_16_file_stream_has_no_getvalue():
test_file = get_fixture("hidden_sheets.xls")
with open(test_file, "rb") as f:
pe.get_sheet(file_stream=f, file_type="xls")
def test_array(self):
for struct_type in ["array", "dict", "records"]:
print("Testing %s" % struct_type)
io = pe.save_as(dest_file_type="xls", array=self.data)
io.seek(0)
response = self.app.post('/exchange/%s' % struct_type,
buffered=True,
data={"file": (io, "test.xls")},
content_type="multipart/form-data")
assert response.content_type == "application/vnd.ms-excel"
sheet = pe.get_sheet(file_type='xls',
file_content=response.data)
assert sheet.to_array() == self.data
eq_(sheet.name, 'test_array')
def main(base_dir):
# "example.csv","example.xlsx","example.ods", "example.xlsm"
spreadsheet = pyexcel.get_sheet(file_name=os.path.join(base_dir,
"example.xls"))
# rows() returns row based iterator, meaning it can be iterated row by row
for row in spreadsheet.rows():
print(row)