Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2cabada

Browse files
committed
Add helper to figure out what type of file you have.
1 parentcdd21f4 commit2cabada

File tree

7 files changed

+83
-1
lines changed

7 files changed

+83
-1
lines changed

‎tests/samples/sample.ods‎

6.73 KB
Binary file not shown.

‎tests/samples/sample.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a text file.

‎tests/samples/sample.xlsb‎

7.39 KB
Binary file not shown.

‎tests/samples/sample.zip‎

170 Bytes
Binary file not shown.

‎tests/test_inspect.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
fromxlrdimportinspect_format
2+
3+
from .helpersimportfrom_sample
4+
5+
6+
deftest_xlsx():
7+
assertinspect_format(from_sample('sample.xlsx'))=='xlsx'
8+
9+
10+
deftest_xlsb():
11+
assertinspect_format(from_sample('sample.xlsb'))=='xlsb'
12+
13+
14+
deftest_ods():
15+
assertinspect_format(from_sample('sample.ods'))=='ods'
16+
17+
18+
deftest_zip():
19+
assertinspect_format(from_sample('sample.zip'))=='zip'
20+
21+
22+
deftest_xls():
23+
assertinspect_format(from_sample('namesdemo.xls'))=='xls'
24+
25+
26+
deftest_content():
27+
withopen(from_sample('sample.xlsx'),'rb')assource:
28+
assertinspect_format(content=source.read())=='xlsx'
29+
30+
31+
deftest_unknown():
32+
assertinspect_format(from_sample('sample.txt'))isNone

‎xlrd/__init__.py‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,62 @@
1212
XL_CELL_NUMBER,XL_CELL_TEXT,XLRDError,biff_text_from_num,
1313
error_text_from_code,
1414
)
15-
from .bookimportBook,colname
15+
from .bookimportBook,colname,open_workbook_xls
16+
from .compdocimportSIGNATUREasXLS_SIGNATURE
1617
from .formulaimport*# is constrained by __all__
1718
from .infoimport__VERSION__,__version__
1819
from .sheetimportempty_cell
1920
from .xldateimportXLDateError,xldate_as_datetime,xldate_as_tuple
2021

2122

23+
#: descriptions of the version file types :mod:`xlrd` can :func:`inspect <inspect_format>`.
24+
FILE_FORMAT_DESCRIPTIONS= {
25+
'xls':'Excel xls',
26+
'xlsb':'Excel 2007 xlsb file',
27+
'xlsx':'Excel xlsx file',
28+
'ods':'Openoffice.org ODS file',
29+
'zip':'Unknown ZIP file',
30+
None:'Unknown file type',
31+
}
32+
33+
ZIP_SIGNATURE=b"PK\x03\x04"
34+
35+
PEEK_SIZE=max(len(XLS_SIGNATURE),len(ZIP_SIGNATURE))
36+
37+
38+
definspect_format(path=None,content=None):
39+
"""
40+
Inspect the supplied path or content stream and return the
41+
file's type as a :class:`str` or ``None`` if it cannot be determined.
42+
"""
43+
ifcontent:
44+
peek=content[:PEEK_SIZE]
45+
else:
46+
path=os.path.expanduser(path)
47+
withopen(path,"rb")asf:
48+
peek=f.read(PEEK_SIZE)
49+
50+
ifpeek.startswith(XLS_SIGNATURE):
51+
return'xls'
52+
53+
ifpeek.startswith(ZIP_SIGNATURE):
54+
zf=zipfile.ZipFile(timemachine.BYTES_IO(content)ifcontentelsepath)
55+
56+
# Workaround for some third party files that use forward slashes and
57+
# lower case names. We map the expected name in lowercase to the
58+
# actual filename in the zip container.
59+
component_names= {name.replace('\\','/').lower():name
60+
fornameinzf.namelist()}
61+
62+
if'xl/workbook.xml'incomponent_names:
63+
return'xlsx'
64+
if'xl/workbook.bin'incomponent_names:
65+
return'xlsb'
66+
if'content.xml'incomponent_names:
67+
return'ods'
68+
return'zip'
69+
70+
2271
defopen_workbook(filename=None,
2372
logfile=sys.stdout,
2473
verbosity=0,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp