Exemple #1
0
def download_file(path: str):
    filename = request.args.get("file")
    format = request.args.get("format")

    if format == "xlsx":
        xlsx_exists = False
        if Path(TMP_PREFIX + filename + ".xlsx").exists():
            xlsx_exists = True
        elif Path(TMP_PREFIX + filename).exists():
            with ApkgReader(TMP_PREFIX + filename) as apkg:
                pyexcel_xlsxwx.save_data(
                    TMP_PREFIX + filename + ".xlsx",
                    apkg.export(),
                    config={"format": None},
                )

            xlsx_exists = True

        if xlsx_exists:
            return send_from_directory(
                os.getcwd(),
                Path(TMP_PREFIX + filename + ".xlsx"),
                as_attachment=True,
                download_name="",
            )
    else:
        if Path(TMP_PREFIX + filename).exists():
            with ApkgReader(TMP_PREFIX + filename) as apkg:
                return jsonify(apkg.export())

    return Response(status=404)
def test_xlsx(in_file, request):
    with ApkgReader(str(Path("tests/input").joinpath(in_file))) as apkg:
        out_file = Path("tests/output").joinpath(request.node.name).with_suffix(".xlsx")
        if out_file.exists():
            out_file.unlink()

        pyexcel_xlsxwx.save_data(str(out_file), apkg.export(), config={"format": None})
        assert out_file.exists()
def test_save(in_file, config, request):
    if isinstance(config, str):
        config = Path("tests/input").joinpath(config)
        assert config.exists()

        config = str(config)

    data = pyexcel.get_book_dict(file_name=str(Path("tests/input").joinpath(in_file)))
    pyexcel_xlsxwx.save_data(
        str(Path("tests/output").joinpath(request.node.name).with_suffix(".xlsx")),
        data,
        config=config,
    )
Exemple #4
0
def export_excel():
    def _dump(table_object):
        headers = table_object.__table__.columns.keys()

        i = headers.index('data')
        headers.pop(i)
        all_keys = set()
        for record in table_object.query:
            if record.data is not None:
                all_keys.update(json.loads(record.data).keys())
        all_keys = list(all_keys)
        headers[i:i] = all_keys

        sheet_data = [headers]
        for record in table_object.query:
            sheet_data.append(_to_minimal_string(record.entry, all_keys))

        return sheet_data

    def _to_minimal_string(v_list, all_keys):
        output = []
        for k, v in v_list.items():
            if k != 'data':
                if isinstance(v, datetime):
                    output.append(v.isoformat())
                else:
                    output.append(v)
            else:
                output2 = [None for _ in all_keys]
                for k2, v2 in json.loads(v).items():
                    if not isinstance(v2, (str, int)):
                        output2[all_keys.index(k2)] = json.dumps(
                            v2, ensure_ascii=False)
                    else:
                        output2[all_keys.index(k2)] = v2
                output.extend(output2)

        return output

    data = OrderedDict()

    data[Hanzi.__tablename__] = _dump(Hanzi)
    data[Vocab.__tablename__] = _dump(Vocab)
    data[Sentence.__tablename__] = _dump(Sentence)

    pyexcel_xlsxwx.save_data(
        'user/HanziLevelUp.xlsx',
        data,
        config={'format': None}
        # config={'format': {'_default': {'num_format': '0'}}}
    )
Exemple #5
0
    def save(self, out_file=None):
        if out_file is None:
            out_file = self.excel

        if not isinstance(out_file, Path):
            out_file = Path(out_file)

        out_matrix = []
        header = [header_item.title() if header_item != 'id' else header_item
                  for header_item in list(self.data.values())[0]._fields]
        out_matrix.append(header)

        for card_id, card_tuple in self.data.items():
            out_matrix.append(list(card_tuple))

        self.all_sheets[self._sheet_name] = out_matrix

        pyexcel_xlsxwx.save_data(out_file=out_file, data=self.all_sheets)
Exemple #6
0
def upload_file(path: str):
    file = request.files["file"]
    filename = file.filename
    format = request.form["format"]

    file.save(TMP_PREFIX + filename)

    Thread(target=lambda: sleep(30) or clear_tmp(filename),
           daemon=True).start()

    with ApkgReader(TMP_PREFIX + filename) as apkg:
        if format == "xlsx":
            pyexcel_xlsxwx.save_data(
                TMP_PREFIX + filename + ".xlsx",
                apkg.export(),
                config={"format": None},
            )

    return Response(status=201)
Exemple #7
0
import pyexcel_xlsxwx

from anki_export import ApkgReader

with ApkgReader("../../tests/input/jp-idiom.apkg") as apkg:
    pyexcel_xlsxwx.save_data("../../tests/output/jp-idiom.xlsx",
                             apkg.export(),
                             config={"format": None})
Exemple #8
0
from anki_export import ApkgReader
import pyexcel_xlsxwx
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import csv
import re
from difflib import SequenceMatcher

with ApkgReader('franka.apkg') as apkg:
    pyexcel_xlsxwx.save_data('test.xlsx', apkg.export(), config={'format': None})

df = pd.read_excel("test.xlsx", sheet_name='Basic')

df['edited'] = df['Front'].map(str)+df['Back']

"""
df['edited'] = df['Text'].map(lambda x: x.replace(' ',' '))
df['edited'] = df['edited'].map(lambda x: x.replace('&;','&'))
df['edited'] = df['edited'].map(lambda x: x.replace('{{',''))
df['edited'] = df['edited'].map(lambda x: x.replace('}}',''))
df['edited'] = df['edited'].map(lambda x: x.replace('c1::',''))
df['edited'] = df['edited'].map(lambda x: x.replace('c2::',''))
df['edited'] = df['edited'].map(lambda x: x.replace(' ',' '))
df['edited'] = df['edited'].map(lambda x: x.replace('<b>',''))
df['edited'] = df['edited'].map(lambda x: x.replace('</b>',''))
df['edited'] = df['edited'].map(lambda x: x.replace('<u>',''))
df['edited'] = df['edited'].map(lambda x: x.replace('</u>',''))
df['edited'] = df['edited'].map(lambda x: x.replace('<i>',''))
df['edited'] = df['edited'].map(lambda x: x.replace('</i>',''))
df['edited'] = df['edited'].map(lambda x: x.replace('{{',''))