# Path: pyxform/xls2json_backends.py
# def is_empty(value):
#     if value is None:
#         return True
#     elif isinstance(value, str) and value.strip() == "":
#         return True
#     else:
#         return False
# 
# def xls_value_to_unicode(value, value_type, datemode):
#     """
#     Take a xls formatted value and try to make a unicode string
#     representation.
#     """
#     if value_type == xlrd.XL_CELL_BOOLEAN:
#         return "TRUE" if value else "FALSE"
#     elif value_type == xlrd.XL_CELL_NUMBER:
#         # Try to display as an int if possible.
#         int_value = int(value)
#         if int_value == value:
#             return str(int_value)
#         else:
#             return str(value)
#     elif value_type is xlrd.XL_CELL_DATE:
#         # Warn that it is better to single quote as a string.
#         # error_location = cellFormatString % (ss_row_idx, ss_col_idx)
#         # raise Exception(
#         #   "Cannot handle excel formatted date at " + error_location)
#         datetime_or_time_only = xlrd.xldate_as_tuple(value, datemode)
#         if datetime_or_time_only[:3] == (0, 0, 0):
#             # must be time only
#             return str(datetime.time(*datetime_or_time_only[3:]))
#         return str(datetime.datetime(*datetime_or_time_only))
#     else:
#         # ensure unicode and replace nbsp spaces with normal ones
#         # to avoid this issue:
#         # https://github.com/modilabs/pyxform/issues/83
#         return str(value).replace(chr(160), " ")
# 
# def xlsx_value_to_str(value):
#     """
#     Take a xls formatted value and try to make a string representation.
#     """
#     if value is True:
#         return "TRUE"
#     elif value is False:
#         return "FALSE"
#     elif isinstance(value, float) and value.is_integer():
#         # Try to display as an int if possible.
#         return str(int(value))
#     elif isinstance(value, (int, datetime.datetime, datetime.time)):
#         return str(value)
#     else:
#         # ensure unicode and replace nbsp spaces with normal ones
#         # to avoid this issue:
#         # https://github.com/modilabs/pyxform/issues/83
#         return str(value).replace(chr(160), " ")


import codecs
import copy
import csv
import json
import os
import re
import openpyxl
import xlrd
from json.decoder import JSONDecodeError
from xml.dom.minidom import Element, Text, parseString
from pyxform.xls2json_backends import is_empty, xls_value_to_unicode, xlsx_value_to_str
                    if m:
                        value = v.value
                        value_type = v.ctype
                        data = xls_value_to_unicode(value, value_type, wb.datemode)
                        # clean the values of leading and trailing whitespaces
                        data = data.strip()
                        csv_data.append(data)
            except TypeError:
                continue
            writer.writerow(csv_data)

    return True


def xlsx_sheet_to_csv(workbook_path, csv_path, sheet_name):
    wb = openpyxl.open(workbook_path)
    try:
        sheet = wb.get_sheet_by_name(sheet_name)
    except KeyError:
        return False
    if sheet.max_row < 2:
        return False
    with open(csv_path, "w", newline="") as f:
        writer = csv.writer(f, quoting=csv.QUOTE_ALL)
        mask = [not is_empty(cell.value) for cell in sheet[1]]
        for row in sheet.rows:
            csv_data = []
            try:
                for v, m in zip(row, mask):
                    if m:

# Path: pyxform/xls2json_backends.py
# def is_empty(value):
#     if value is None:
#         return True
#     elif isinstance(value, str) and value.strip() == "":
#         return True
#     else:
#         return False
# 
# def xls_value_to_unicode(value, value_type, datemode):
#     """
#     Take a xls formatted value and try to make a unicode string
#     representation.
#     """
#     if value_type == xlrd.XL_CELL_BOOLEAN:
#         return "TRUE" if value else "FALSE"
#     elif value_type == xlrd.XL_CELL_NUMBER:
#         # Try to display as an int if possible.
#         int_value = int(value)
#         if int_value == value:
#             return str(int_value)
#         else:
#             return str(value)
#     elif value_type is xlrd.XL_CELL_DATE:
#         # Warn that it is better to single quote as a string.
#         # error_location = cellFormatString % (ss_row_idx, ss_col_idx)
#         # raise Exception(
#         #   "Cannot handle excel formatted date at " + error_location)
#         datetime_or_time_only = xlrd.xldate_as_tuple(value, datemode)
#         if datetime_or_time_only[:3] == (0, 0, 0):
#             # must be time only
#             return str(datetime.time(*datetime_or_time_only[3:]))
#         return str(datetime.datetime(*datetime_or_time_only))
#     else:
#         # ensure unicode and replace nbsp spaces with normal ones
#         # to avoid this issue:
#         # https://github.com/modilabs/pyxform/issues/83
#         return str(value).replace(chr(160), " ")
# 
# def xlsx_value_to_str(value):
#     """
#     Take a xls formatted value and try to make a string representation.
#     """
#     if value is True:
#         return "TRUE"
#     elif value is False:
#         return "FALSE"
#     elif isinstance(value, float) and value.is_integer():
#         # Try to display as an int if possible.
#         return str(int(value))
#     elif isinstance(value, (int, datetime.datetime, datetime.time)):
#         return str(value)
#     else:
#         # ensure unicode and replace nbsp spaces with normal ones
#         # to avoid this issue:
#         # https://github.com/modilabs/pyxform/issues/83
#         return str(value).replace(chr(160), " ")


# Path: pyxform/utils.py
import codecs
import copy
import csv
import json
import os
import re
import openpyxl
import xlrd
from json.decoder import JSONDecodeError
from xml.dom.minidom import Element, Text, parseString
from pyxform.xls2json_backends import is_empty, xls_value_to_unicode, xlsx_value_to_str

                    if m:
                        value = v.value
                        value_type = v.ctype
                        data = xls_value_to_unicode(value, value_type, wb.datemode)
                        # clean the values of leading and trailing whitespaces
                        data = data.strip()
                        csv_data.append(data)
            except TypeError:
                continue
            writer.writerow(csv_data)

    return True


def xlsx_sheet_to_csv(workbook_path, csv_path, sheet_name):
    wb = openpyxl.open(workbook_path)
    try:
        sheet = wb.get_sheet_by_name(sheet_name)
    except KeyError:
        return False
    if sheet.max_row < 2:
        return False
    with open(csv_path, "w", newline="") as f:
        writer = csv.writer(f, quoting=csv.QUOTE_ALL)
        mask = [not is_empty(cell.value) for cell in sheet[1]]
        for row in sheet.rows:
            csv_data = []
            try:
                for v, m in zip(row, mask):
                    if m:

                        data = xlsx_value_to_str(v.value)