Skip to content

Commit

Permalink
v1.48 fixed openpyxl generated read/write errors
Browse files Browse the repository at this point in the history
  • Loading branch information
PydPiper committed Nov 19, 2020
1 parent 72597ab commit 759b51a
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 18 deletions.
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,12 @@ and/or download restrictions, see [docs - installation](https://pylightxl.readth

---

#### **pypi version 1.47**

- added new function: ``db.nr('table1')`` returns the contents of named range "table1"
- added new function: ``db.ws('Sheet1').range('A1:C3')`` that returns the contents of a range
it also has the ability to return the formulas of the range
- updated ``db.ws('Sheet1').row()`` and ``db.ws('Sheet1').col()`` to take in a new argument ``formual``
that returns the formulas of a row or col
- bugfix: write to existing without named ranges was throwing a "repair" error. Fixed typo on xml for it
and added unit tests to capture it
- added new function: ``xl.readcsv(fn, delimiter, ws)`` to read csv files and create a pylightxl db out
of it (type converted)
- added new function: ``xl.writecsv(db, fn, ws, delimiter)`` to write out a pylightxl worksheet as a csv
#### **pypi version 1.48**

- add feature to ``writecsv`` to be able to handle ``pathlib`` object and ``io.StreamIO`` object
- refactored readxl to remove regex, now readxl is all cElementTree
- refactored readxl/writexl to able to handle excel files written by openpyxl that is generated
differently than how excel write files.

See full history log of revisions: [Here](https://pylightxl.readthedocs.io/en/latest/revlog.html)

Expand Down
1 change: 0 additions & 1 deletion _config.yml

This file was deleted.

8 changes: 7 additions & 1 deletion doc/source/revlog.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
Revision Log
============

pypi version 1.48 (in-work)
pypi version 1.49 (in-work)
---------------------------
- speed improvements

pypi version 1.48
-----------------
- add feature to ``writecsv`` to be able to handle ``pathlib`` object and ``io.StreamIO`` object
- refactored readxl to remove regex, now readxl is all cElementTree
- refactored readxl/writexl to able to handle excel files written by openpyxl that is generated
differently than how excel write files.

pypi version 1.47
-----------------
Expand Down
18 changes: 15 additions & 3 deletions pylightxl/pylightxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ def readxl_get_workbookxmlrels(fn):

for relationship in root.findall('./default:Relationship', ns):
fn_ws = relationship.get('Target')
# openpyxl write its xl/_rels/workbook.xml.rels file differently than excel itself. It adds on /xl/ at the start of the file path
if fn_ws[:4] == '/xl/':
fn_ws = fn_ws[4:]
rId = relationship.get('Id')
rv[rId] = fn_ws

Expand Down Expand Up @@ -570,7 +573,12 @@ def writexl_alt_app_text(db, filepath):

if db.nr_names == {}:
# does not contain namedranges
tag_vt_vector = root.find('./default:HeadingPairs//vt:vector', ns)
try:
tag_vt_vector = root.find('./default:HeadingPairs//vt:vector', ns)
except SyntaxError:
# this occurs when excel file was created by another program like openpyxl
# where not all information was written to docProps/app.xml
return writexl_new_app_text(db)
tag_vt_vector.clear()
tag_vt_vector.set('size', '2')
tag_vt_vector.set('baseType', 'variant')
Expand All @@ -587,10 +595,14 @@ def writexl_alt_app_text(db, filepath):
tag_vt_lpstr.text = str(len(db.ws_names))
tag_vt_variant.append(tag_vt_lpstr)


else:
# contains namedranges
tag_vt_vector = root.find('./default:HeadingPairs//vt:vector', ns)
try:
tag_vt_vector = root.find('./default:HeadingPairs//vt:vector', ns)
except SyntaxError:
# this occurs when excel file was created by another program like openpyxl
# where not all information was written to docProps/app.xml
return writexl_new_app_text(db)
tag_vt_vector.clear()
tag_vt_vector.set('size', '4')
tag_vt_vector.set('baseType', 'variant')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pylightxl", # Replace with your own username
version="1.47",
version="1.48",
author="Viktor Kis",
author_email="",
description="A light weight excel read/writer for python27 and python3 with no dependencies",
Expand Down
11 changes: 11 additions & 0 deletions test/_pylightxl_newopenpyxl.xlsx/[Content_Types].xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
</Types>
Expand Down
6 changes: 6 additions & 0 deletions test/_pylightxl_newopenpyxl.xlsx/_rels/.rels
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
</Relationships>
15 changes: 15 additions & 0 deletions test/_pylightxl_newopenpyxl.xlsx/docProps/app.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
<Application>Microsoft Excel</Application>
<DocSecurity>0</DocSecurity>
<ScaleCrop>false</ScaleCrop>
<HeadingPairs>
<vt:vector size="2" baseType="variant"><vt:variant><vt:lpstr>Worksheets</vt:lpstr></vt:variant><vt:variant><vt:i4>1</vt:i4></vt:variant></vt:vector></HeadingPairs>
<TitlesOfParts>
<vt:vector size="1" baseType="lpstr"><vt:lpstr>Sheet</vt:lpstr></vt:vector></TitlesOfParts>
<Company />
<LinksUpToDate>false</LinksUpToDate>
<SharedDoc>false</SharedDoc>
<HyperlinksChanged>false</HyperlinksChanged>
<AppVersion>16.0300</AppVersion>
</Properties>
7 changes: 7 additions & 0 deletions test/_pylightxl_newopenpyxl.xlsx/docProps/core.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties">
<dc:creator>pylightxl</dc:creator>
<cp:lastModifiedBy>pylightxl</cp:lastModifiedBy>
<dcterms:created xsi:type="dcterms:W3CDTF">2019-12-27T01:35:28Z</dcterms:created>
<dcterms:modified xsi:type="dcterms:W3CDTF">2019-12-27T01:35:39Z</dcterms:modified>
</cp:coreProperties>
6 changes: 6 additions & 0 deletions test/_pylightxl_newopenpyxl.xlsx/xl/_rels/workbook.xml.rels
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Target="worksheets/sheet1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Id="rId1"/>
</Relationships>
Expand Down
4 changes: 4 additions & 0 deletions test/_pylightxl_newopenpyxl.xlsx/xl/sharedStrings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst uniqueCount="0" count="0" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
</sst>
Expand Down
10 changes: 10 additions & 0 deletions test/_pylightxl_newopenpyxl.xlsx/xl/workbook.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x15 xr xr6 xr10 xr2" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr6="http://schemas.microsoft.com/office/spreadsheetml/2016/revision6" xmlns:xr10="http://schemas.microsoft.com/office/spreadsheetml/2016/revision10" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2">
<fileVersion appName="xl" lastEdited="7" lowestEdited="7" rupBuild="22228"/>
<workbookPr defaultThemeVersion="166925"/>
<sheets>
<sheet name="Sheet" sheetId="1" r:id="rId1"/>
</sheets>
<calcPr calcId="181029"/>
</workbook>
Expand Down
13 changes: 13 additions & 0 deletions test/_pylightxl_newopenpyxl.xlsx/xl/worksheets/sheet1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac xr xr2 xr3" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" xr:uid="2C7EE24B-C535-494D-AA97-0A61EE84BA40">
<dimension ref="A1"/>
<sheetViews>
<sheetView tabSelected="1" workbookViewId="0"/>
</sheetViews>
<sheetFormatPr defaultRowHeight="15" x14ac:dyDescent="0.25"/>
<sheetData>
<row r="1" x14ac:dyDescent="0.25" spans="1:1"><c r="A1" ><v>42</v></c></row>
</sheetData>
<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
</worksheet>
Expand Down
Binary file added test/newopenpyxl.xlsx
Binary file not shown.
Binary file added test/openpyxl.xlsx
Binary file not shown.
7 changes: 7 additions & 0 deletions test/test_writexl.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ def test_new_content_types_text(self):
db._sharedStrings = ['text']
self.assertEqual(xl.writexl_new_content_types_text(db), xml_base.format(many_tag_sheets=many_tag_sheets, tag_sharedStrings=xml_tag_sharedStrings))

def test_openpyxl(self):
# test that pylightxl is able to write to a openpyxl output excel file (docProps/app.xml) is different than expected
db = xl.readxl('openpyxl.xlsx')

xl.writexl(db, 'newopenpyxl.xlsx')



class TestWritexlExisting(TestCase):

Expand Down

0 comments on commit 759b51a

Please sign in to comment.