Ver a proveniência

fix: Parse .xls and .xlsx files in import

version-14
Faris Ansari há 5 anos
ascendente
cometimento
07eedad5bb
3 ficheiros alterados com 26 adições e 3 eliminações
  1. +14
    -3
      frappe/core/doctype/data_import/importer_new.py
  2. +11
    -0
      frappe/utils/xlsxutils.py
  3. +1
    -0
      requirements.txt

+ 14
- 3
frappe/core/doctype/data_import/importer_new.py Ver ficheiro

@@ -11,6 +11,10 @@ from datetime import datetime
from frappe import _
from frappe.utils import cint, flt, DATE_FORMAT, DATETIME_FORMAT
from frappe.utils.csvutils import read_csv_content
from frappe.utils.xlsxutils import (
read_xlsx_file_from_attached_file,
read_xls_file_from_attached_file,
)
from frappe.exceptions import ValidationError, MandatoryError
from frappe.model import display_fieldtypes, no_value_fields, table_fields

@@ -45,11 +49,12 @@ class Importer:
if self.data_import:
file_doc = frappe.get_doc("File", {"file_url": self.data_import.import_file})
content = file_doc.get_content()
extension = file_doc.file_name.split(".")[1]

if file_path:
self.read_file(file_path)
elif content:
self.read_content(content)
self.read_content(content, extension)
self.remove_empty_rows_and_columns()

def read_file(self, file_path):
@@ -64,8 +69,14 @@ class Importer:
self.header_row = data[0]
self.data = data[1:]

def read_content(self, content):
data = read_csv_content(content)
def read_content(self, content, extension):
if extension == "csv":
data = read_csv_content(content)
elif extension == "xlsx":
data = read_xlsx_file_from_attached_file(fcontent=content)
elif extension == "xls":
data = read_xls_file_from_attached_file(content)

self.header_row = data[0]
self.data = data[1:]



+ 11
- 0
frappe/utils/xlsxutils.py Ver ficheiro

@@ -5,6 +5,7 @@ from __future__ import unicode_literals
import frappe

import openpyxl
import xlrd
import re
from openpyxl.styles import Font
from openpyxl import load_workbook
@@ -95,3 +96,13 @@ def read_xlsx_file_from_attached_file(file_url=None, fcontent=None, filepath=Non
tmp_list.append(cell.value)
rows.append(tmp_list)
return rows

def read_xls_file_from_attached_file(content):
book = xlrd.open_workbook(file_contents=content)
sheets = book.sheets()
sheet = sheets[0]
rows = []
for i in range(sheet.nrows):
rows.append(sheet.row_values(i))
rows = rows[1:]
return rows

+ 1
- 0
requirements.txt Ver ficheiro

@@ -64,3 +64,4 @@ sqlparse==0.2.4
Pygments==2.2.0
frontmatter
PyYAML==3.13
xlrd

Carregando…
Cancelar
Guardar