Add support for importing spreadsheets seamlessly.
This commit is contained in:
@@ -541,22 +541,26 @@ class MenuBar(QtWidgets.QMenuBar):
|
|||||||
self.parent().LENTDB.addEntity(existingEntity, updateTimeline=False)
|
self.parent().LENTDB.addEntity(existingEntity, updateTimeline=False)
|
||||||
|
|
||||||
elif importDialog.CSVFileChoice.isChecked():
|
elif importDialog.CSVFileChoice.isChecked():
|
||||||
csvDF = pd.read_csv(fileDirectory)
|
try:
|
||||||
|
csvDF = pd.read_excel(fileDirectory)
|
||||||
|
except ValueError:
|
||||||
|
csvDF = pd.read_csv(fileDirectory)
|
||||||
|
|
||||||
|
# If we have no rows or columns, we cannot import anything.
|
||||||
|
# This is essentially a sanity check.
|
||||||
|
if len(csvDF.index) < 1:
|
||||||
|
raise ValueError("Invalid import file data - Not enough rows.")
|
||||||
|
if len(csvDF.columns) < 1:
|
||||||
|
raise ValueError("Invalid import file data - Not enough columns.")
|
||||||
|
|
||||||
# Remove duplicate column names
|
# Remove duplicate column names
|
||||||
|
# When importing, all columns should be given unique values,
|
||||||
|
# so this should be just another sanity check.
|
||||||
csvDF = csvDF.loc[:, ~csvDF.columns.duplicated()]
|
csvDF = csvDF.loc[:, ~csvDF.columns.duplicated()]
|
||||||
|
|
||||||
# Fill NaN values with an empty string
|
# Fill NaN values with an empty string
|
||||||
csvDF.fillna('')
|
csvDF.fillna('')
|
||||||
|
|
||||||
# If we have less than 2 rows, we cannot import
|
|
||||||
rowNumber = len(csvDF.index)
|
|
||||||
if rowNumber < 2:
|
|
||||||
raise ValueError("Invalid CSV file data - Not enough rows.")
|
|
||||||
|
|
||||||
if len(csvDF.columns) < 1:
|
|
||||||
raise ValueError("Invalid CSV file data - Not enough columns.")
|
|
||||||
|
|
||||||
importEntityCSVDialog = ImportEntityFromCSVFile(self, csvDF)
|
importEntityCSVDialog = ImportEntityFromCSVFile(self, csvDF)
|
||||||
if importEntityCSVDialog.exec_():
|
if importEntityCSVDialog.exec_():
|
||||||
attributeRows = [comboBox.currentText()
|
attributeRows = [comboBox.currentText()
|
||||||
@@ -588,22 +592,26 @@ class MenuBar(QtWidgets.QMenuBar):
|
|||||||
self.parent().LENTDB.addEntity(existingEntity, updateTimeline=False)
|
self.parent().LENTDB.addEntity(existingEntity, updateTimeline=False)
|
||||||
|
|
||||||
elif importDialog.CSVFileChoiceLinks.isChecked():
|
elif importDialog.CSVFileChoiceLinks.isChecked():
|
||||||
csvDF = pd.read_csv(fileDirectory)
|
try:
|
||||||
|
csvDF = pd.read_excel(fileDirectory)
|
||||||
|
except ValueError:
|
||||||
|
csvDF = pd.read_csv(fileDirectory)
|
||||||
|
|
||||||
|
# If we have no rows or columns, we cannot import anything.
|
||||||
|
# This is essentially a sanity check.
|
||||||
|
if len(csvDF.index) < 1:
|
||||||
|
raise ValueError("Invalid import file data - Not enough rows.")
|
||||||
|
if len(csvDF.columns) < 1:
|
||||||
|
raise ValueError("Invalid import file data - Not enough columns.")
|
||||||
|
|
||||||
# Remove duplicate column names
|
# Remove duplicate column names
|
||||||
|
# When importing, all columns should be given unique values,
|
||||||
|
# so this should be just another sanity check.
|
||||||
csvDF = csvDF.loc[:, ~csvDF.columns.duplicated()]
|
csvDF = csvDF.loc[:, ~csvDF.columns.duplicated()]
|
||||||
|
|
||||||
# Fill NaN values with an empty string
|
# Fill NaN values with an empty string
|
||||||
csvDF.fillna('')
|
csvDF.fillna('')
|
||||||
|
|
||||||
# If we have less than 2 rows, we cannot import
|
|
||||||
rowNumber = len(csvDF.index)
|
|
||||||
if rowNumber < 2:
|
|
||||||
raise ValueError("Invalid CSV file data - Not enough rows.")
|
|
||||||
|
|
||||||
if len(csvDF.columns) < 1:
|
|
||||||
raise ValueError("Invalid CSV file data - Not enough columns.")
|
|
||||||
|
|
||||||
importLinksCSVDialog = ImportLinksFromCSVFile(self, csvDF)
|
importLinksCSVDialog = ImportLinksFromCSVFile(self, csvDF)
|
||||||
if importLinksCSVDialog.exec_():
|
if importLinksCSVDialog.exec_():
|
||||||
unmapped = []
|
unmapped = []
|
||||||
@@ -2201,10 +2209,11 @@ class ImportEntityFromCSVFile(QtWidgets.QDialog):
|
|||||||
class ImportFromFileDialog(QtWidgets.QDialog):
|
class ImportFromFileDialog(QtWidgets.QDialog):
|
||||||
|
|
||||||
def popupFileDialog(self):
|
def popupFileDialog(self):
|
||||||
self.fileDirectory = QtWidgets.QFileDialog().getOpenFileName(parent=self, caption='Select File to Import From',
|
self.fileDirectory = QtWidgets.QFileDialog().getOpenFileName(
|
||||||
dir=str(Path.home()),
|
parent=self, caption='Select File to Import From',
|
||||||
options=QtWidgets.QFileDialog.DontUseNativeDialog,
|
dir=str(Path.home()),
|
||||||
filter="CSV or txt (*.csv *.txt)")[0]
|
options=QtWidgets.QFileDialog.DontUseNativeDialog,
|
||||||
|
filter="Import File (*.csv *.txt *.xls *.xlsx *.ods)")[0]
|
||||||
if self.fileDirectory != '':
|
if self.fileDirectory != '':
|
||||||
self.fileDirectoryLine.setText(self.fileDirectory)
|
self.fileDirectoryLine.setText(self.fileDirectory)
|
||||||
|
|
||||||
@@ -2232,9 +2241,9 @@ class ImportFromFileDialog(QtWidgets.QDialog):
|
|||||||
self.textFileChoice = QtWidgets.QRadioButton('Text file - Entities Import')
|
self.textFileChoice = QtWidgets.QRadioButton('Text file - Entities Import')
|
||||||
self.textFileChoice.setStyleSheet(Stylesheets.RADIO_BUTTON_STYLESHEET)
|
self.textFileChoice.setStyleSheet(Stylesheets.RADIO_BUTTON_STYLESHEET)
|
||||||
self.textFileChoice.setChecked(True)
|
self.textFileChoice.setChecked(True)
|
||||||
self.CSVFileChoice = QtWidgets.QRadioButton('CSV - Entities Import')
|
self.CSVFileChoice = QtWidgets.QRadioButton('Spreadsheet / CSV - Entities Import')
|
||||||
self.CSVFileChoice.setStyleSheet(Stylesheets.RADIO_BUTTON_STYLESHEET)
|
self.CSVFileChoice.setStyleSheet(Stylesheets.RADIO_BUTTON_STYLESHEET)
|
||||||
self.CSVFileChoiceLinks = QtWidgets.QRadioButton('CSV - Links Import')
|
self.CSVFileChoiceLinks = QtWidgets.QRadioButton('Spreadsheet / CSV - Links Import')
|
||||||
self.CSVFileChoiceLinks.setStyleSheet(Stylesheets.RADIO_BUTTON_STYLESHEET)
|
self.CSVFileChoiceLinks.setStyleSheet(Stylesheets.RADIO_BUTTON_STYLESHEET)
|
||||||
|
|
||||||
dialogLayout.addWidget(self.fileDirectoryLine, 1, 0, 1, 2)
|
dialogLayout.addWidget(self.fileDirectoryLine, 1, 0, 1, 2)
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ reportlab
|
|||||||
pandas
|
pandas
|
||||||
svglib
|
svglib
|
||||||
lxml
|
lxml
|
||||||
|
# For opening spreadsheets
|
||||||
|
odfpy
|
||||||
|
openpyxl
|
||||||
|
xlrd # For old Excel formats
|
||||||
# Speeds up parsing of webpages by bs4
|
# Speeds up parsing of webpages by bs4
|
||||||
cchardet
|
cchardet
|
||||||
bs4
|
bs4
|
||||||
|
|||||||
Reference in New Issue
Block a user