GithubHelp home page GithubHelp logo

xlsx2csv's Introduction

xlsx2csv

Simple script for converting xlsx files to csv files commandline.

Usage

We provide compiled version of the program for Windows Mac OS and Linux.

Usage:
	./xlsx2csv_mac_64 [flags] <xlsx-to-be-read>
  -d string
    	Delimiter to use between fields (default ";")
  -i int
    	Index of sheet to convert, zero based
  -o string
    	filename to output to. -=stdout (default "-")

xlsx2csv's People

Contributors

efeakaroz13 avatar hernan43 avatar shawnmilo avatar tealeg avatar tgulacsi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xlsx2csv's Issues

I added argument -s Sheetname

Sometimes it's easiert to make order to convert using Sheetname. Added param sheetName. Added open sheet using SheetName if not empty string.

package main

import (
        "errors"
        "flag"
        "fmt"
        "os"
        "strings"

        "github.com/tealeg/xlsx"
)

var xlsxPath = flag.String("f", "", "Path to an XLSX file")
var sheetIndex = flag.Int("i", 0, "Index of sheet to convert, zero based")
var sheetName = flag.String("s", "", "Name of sheet to convert")
var delimiter = flag.String("d", ";", "Delimiter to use between fields")

type outputer func(s string)

func generateCSVFromXLSXFile(excelFileName string, sheetIndex int, sheetName string, outputf outputer) error {                                              xlFile, error := xlsx.OpenFile(excelFileName)
        if error != nil {
                return error
        }
        sheetLen := len(xlFile.Sheets)
        switch {
        case sheetLen == 0:
                return errors.New("This XLSX file contains no sheets.")
        case sheetIndex >= sheetLen:
                return fmt.Errorf("No sheet %d available, please select a sheet between 0 and %d\n", sheetIndex, sheetLen-1)
        }
        sheet := xlFile.Sheets[sheetIndex]
        // or like to use sheet name ?
        if sheetName != ""  {
                sheet2, ok := xlFile.Sheet[sheetName]                                                                                                              
                if ok != true {                                                                                                                                             
                      return errors.New("This XLSX file contains not named sheet.")                                                                               
                }
                sheet=sheet2
        }
        for _, row := range sheet.Rows {
                var vals []string
                if row != nil {
                        for _, cell := range row.Cells {
                                str, err := cell.FormattedValue()
                                if err != nil {
                                        vals = append(vals, err.Error())
                                }
                                vals = append(vals, fmt.Sprintf("%q", str))
                        }
                        outputf(strings.Join(vals, *delimiter) + "\n")
                }
        }
        return nil
}

func main() {
        flag.Parse()
        if len(os.Args) < 3 {
                flag.PrintDefaults()
                return
        }
        flag.Parse()
        printer := func(s string) { fmt.Printf("%s", s) }
        if err := generateCSVFromXLSXFile(*xlsxPath, *sheetIndex, *sheetName, printer); err != nil {
                fmt.Println(err)
        }
}

three quotes on export

When in run de program:
go run main.go testdata/testfile.xlsx

I get the following result:

"""Foo""";"""Bar"""
"""Baz """;"""Quuk"""

I'm newbie in go but reading the code I see in line 45:

vals = append(vals, fmt.Sprintf("%q", str))

the %q add quotes but go cw.Write(vals) add quotes too so the result is a lot of quotes ๐Ÿ˜„

golang writer.go line 89

Fail to open xlsx which created by NewStreamFileBuilder method

runtime error: index out of range

download test.xlsx and then open the test.xlsx. a runtime error occur
here is my example.
anyone knows how to fix it

import (
	"fmt"
	"log"
	"net/http"

	"github.com/tealeg/xlsx"
)

func main() {
	oexcel := func(w http.ResponseWriter, req *http.Request) {
		_, err := xlsx.OpenFile("/tmp/test.xlsx") // here open you local file path
		if err != nil {
			fmt.Println(err)
			return
		}

		fmt.Println("no err")
	}

	dexcel := func(w http.ResponseWriter, req *http.Request) {
		headers := []string{"groupName", "groupID", "groupPath", "groupOwner"}
		filename := "test"
		w.Header().Add("Content-Disposition", "attachment;filename=\""+filename+".xlsx\"")
		w.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8")
		w.Header().Set("X-Content-Type-Options", "nosniff")
		b := xlsx.NewStreamFileBuilder(w)
		cellType := xlsx.CellTypeString
		ct := []*xlsx.CellType{cellType.Ptr(), cellType.Ptr(), cellType.Ptr(), cellType.Ptr()}
		if err := b.AddSheet("Sheet1", headers, ct); err != nil {
			fmt.Println("end")
			return
		}

		sf, err := b.Build()
		fmt.Println(sf, err)
		if err != nil {
			fmt.Println("end")
			return
		}

		defer sf.Close()
		for i := 0; i < 100; i++ {
			sf.Write([]string{"GroupName", "GroupID", "PathName", "OwnerAccount"})
		}
		return
	}

	http.HandleFunc("/openExcel", oexcel)
	http.HandleFunc("/downloadExcel", dexcel)
	log.Fatal(http.ListenAndServe(":8080", nil))
}

go get not working for xlsx2csv

can't use go get because of an issue with the Sheet type in xlsx

go get github.com/tealeg/xlsx2csv
# github.com/tealeg/xlsx2csv
../../go/src/github.com/tealeg/xlsx2csv/main.go:37:27: sheet.Rows undefined (type *xlsx.Sheet has no field or method Rows)

Function only reading filtered rows. How do I read all rows from a sheet?

Here is my code -
filepath = os.path.join(FolderPath, FileName)
a = load_workbook(filepath, read_only=True)

def read_excel_file(path: str, sheet_index: int) -> pd.DataFrame:
buffer = StringIO()
Xlsx2csv(path, outputencoding="utf8").convert(buffer,sheetid=sheet_index)
buffer.seek(0)
df = pd.read_csv(buffer)
return df

dfinal=read_excel_file(filepath,a.index(a.get_sheet_by_name('Sheetname'))+1)

Unicode characters don't always output correctly.

It seems that sometimes Unicode characters don't output correctly. Go is usually pretty good at doing the right thing here, so I suspect this might just be to do with output, but it requires investigation and is a blocker for my use of this program.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.