Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a excel file only with python standard library?

Tags:

python

excel

I am new to Python.

I use putty to manage some servers. I want to use Python to create a Excel file on each server, for that I think if I can use some orders like ssh ip "python abc.py" to create the file. It is possible to write a bash script to manage all servers. This is the trouble I meet:

The servers can't use internet.

And it is not allowed to use any third party libraries. When a linux(redhat 6.5) was installed, is there any library in python that can be used to create Excel immediately? Please help me, thanks.

like image 311
Z.Lun Avatar asked Dec 06 '22 15:12

Z.Lun


2 Answers

Actually there is a way. if you have an empty excel file, you can open it as a zip file and this is the content

    .
    ├── [Content_Types].xml
    ├── docProps
    │   ├── app.xml
    │   ├── core.xml
    │   └── custom.xml
    ├── _rels
    └── xl
        ├── calcChain.xml
        ├── charts
        │   ├── chart1.xml
        │   ├── chart2.xml
        │   ├── colors1.xml
        │   ├── colors2.xml
        │   ├── _rels
        │   │   ├── chart1.xml.rels
        │   │   └── chart2.xml.rels
        │   ├── style1.xml
        │   └── style2.xml
        ├── drawings
        │   ├── drawing1.xml
        │   ├── drawing2.xml
        │   └── _rels
        │       ├── drawing1.xml.rels
        │       └── drawing2.xml.rels
        ├── printerSettings
        │   └── printerSettings1.bin
        ├── _rels
        │   └── workbook.xml.rels
        ├── sharedStrings.xml
        ├── styles.xml
        ├── theme
        │   └── theme1.xml
        ├── workbook.xml
        └── worksheets
            ├── _rels
            │   ├── sheet1.xml.rels
            │   └── sheet2.xml.rels
            ├── sheet1.xml
            └── sheet2.xml

On this file I had two sheets which compare to the tab on the workbook.

the workbook.xml, in the xl directory, has the names of the sheets on it and it looks like this:

<sheets><sheet name="Test1" sheetId="1" r:id="rId1"/><sheet name="Test2" sheetId="2" r:id="rId2"/></sheets>

and each sheet(1/2).xml, in the worksheet directory contains the columns/rows
and it looks like this:

<dimension ref="A1:J1352"/>
   <sheetViews>
      <sheetView topLeftCell="A3" workbookViewId="0">
         <selection activeCell="F28" sqref="F28"/>
      </sheetView>
   </sheetViews>
   <sheetFormatPr defaultRowHeight="14.4" x14ac:dyDescent="0.3"/>
   <cols>
      <col min="1" max="1" width="27.6640625" customWidth="1"/>
      <col min="2" max="2" width="15.109375" customWidth="1"/>
      <col min="3" max="3" width="14.6640625" customWidth="1"/>
      <col min="4" max="4" width="15.33203125" customWidth="1"/>
      <col min="7" max="7" width="13.6640625" customWidth="1"/>
   </cols>
   <sheetData>
      <row r="1" spans="1:10" x14ac:dyDescent="0.3"> 
          <c r="A1" t="s">
              <v>1</v>
          </c>
      </row>
      <row r="2" spans="1:10" x14ac:dyDescent="0.3">
         <c r="B2" t="s"> 
            <v>4</v> 
         </c>
         <c r="C2" t="s">
            <v>5</v> 
         </c>
         <c r="D2" t="s">
            <v>6</v>
         </c>
      </row>
      <row r="3" spans="1:10" x14ac:dyDescent="0.3">
         <c r="A3" s="1">
            <v>42969.681041666663</v>
         </c>
         <c r="B3"> 
            <v>892.76</v>
         </c>
         <c r="C3">
            <v>138.62</v>
         </c>
         <c r="D3">
            <v>138.62</v>
         </c>
      </row>
      <row r="4" spans="1:10" x14ac:dyDescent="0.3">
         <c r="A4" s="1">
           <v>42969.68173611111</v>
         </c>
         <c r="B4">
           <v>954.83</v>
         </c>
         <c r="C4">
           <v>88.97</v>
         </c>
         <c r="D4">
           <v>88.97</v>
         </c>
         <c r="G4" s="4"/>
         <c r="H4" s="3" t="s">
           <v>9</v>
         </c>
         <c r="I4" s="3" t="s">
           <v>10</v>
         </c>
         <c r="J4" s="3" t="s">
           <v>11</v>
         </c>
      </row>
      <row r="5" spans="1:10" x14ac:dyDescent="0.3">
         <c r="A5" s="1">
           <v>42969.682430555556</v>
         </c>
         <c r="B5">
           <v>875.17</v>
         </c>
         <c r="C5">
           <v>94.14</v>
         </c>
         <c r="D5">
           <v>94.14</v>
         </c>
         <c r="G5" s="3" t="s">
           <v>2</v>
         </c>
         <c r="H5" s="3">
           <f>AVERAGE(B3:B1352)</f>
           <v>699.96431851851924</v>
         </c>
         <c r="I5" s="3">
           <f>MAX(B3:B1352)</f>
           <v>1270.3399999999999</v>
         </c>
         <c r="J5" s="3">
           <f>MIN(B3:B1352)</f>
           <v>177.93</v>
         </c>
      </row>
   </sheetData>
   <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
   <pageSetup orientation="portrait" r:id="rId1"/>
   <drawing r:id="rId2"/>
</worksheet>

So if you can modify the xml files using the tags correctly, you can alter or create a excel worksheet just using a text editor and a compression program

like image 165
Raxl Avatar answered Mar 23 '23 16:03

Raxl


Unfortunately it is not possible as this. Creating Excel files may require two parts running on the client-side:

  • Office Excel installed (can be relaxed if Open XML format is supported);
  • Python Wrapper for Excel such as xrld or openpyxls.

Python Standard Library does not include wrapper for Office products. Instead they provide commodities for Comma Separated Value files that are easily readable with Excel.

The Openpyxl project works with Open XML format and then there is no need for Office to be installed. Citing its documentation:

Openpyxl is a Python library for reading and writing Excel 2010 xlsx/xlsm/xltx/xltm files.

It was born from lack of existing library to read/write natively from Python the Office Open XML format.

Then you just have to update your Python setup with this package to be able to create spreadsheet.

If your server cannot access internet but accept external media to be mounted, then you can download source on another machine and store it on a removable medium, mount the medium on your server, then decompress it and install it using python setup.py interface:

tar -xvzf openpyxl.tar.gz /somepath
cd /somepath/to/installer
python setup.py install

It works on Debian (no office installed, using pipy source):

>>> import openpyxl
>>> wb = openpyxl.Workbook()
>>> wb.save('test.xlsx')

Produces an empty spreadsheet readable by Excel.

like image 42
jlandercy Avatar answered Mar 23 '23 17:03

jlandercy