Writing Keynotes file from Excel with Python

So I’ve been working on a tool that looks and selects data from an Excel database and then writes a txt. file with the necessary Keynotes Revit syntax.
The objective is that users can generate Keynotes text files by editing some Dynamo Player input nodes without having to know how Keynotes work. Then they can import them into Revit.
For the first time I am taking advantage of pandas and numpy python libraries to achieve what I want, but I am getting some unexpected results in Revit.

Left result:

  1. generated .txt file from Dynamo

Right result:

  1. generated excel file from Dynamo;
  2. opened file and selected the function “wrap text” on the specific column;
  3. saved as unicode format.

As you can see there is a line break after the word “gevel” and I cannot understand the cause of it.
Both of Keynote tags instances are the same family and type with the option “Wrap between parameters only” set to False. The string clearly didn’t reach the label’s boundary but there is still a forced line break.


(image of the output from both txt files)
As you see there is a syntax difference between both text files and unfortunately I wasn’t able to reproduce the right output syntax on my code.

Final lines of code to export the Excel file

  # Seting up excel file
  final_concat_excel = pd.concat(dataframe_list)
  excel_file = r'V:\Revit\09-dynamo scripts\97-developing\07-Keynotes\result_1.xlsx'
  excel_sheet_name = "Keynotes_automatically gen"
  final_concat_excel.to_excel(excel_file, excel_sheet_name, header=False, index=False)

Final lines of code to export the Text file

# Setting up .txt file
final_concat_txt_file = pd.concat(dataframe_list)
final_concat_txt_file = final_concat_txt_file.replace('\n','\r', regex=True) # Replacing new lines with carriage returns
text = np.savetxt(text_path_output, final_concat_txt_file.values, fmt='%s' , delimiter='\t', encoding="utf-8") # Added encoding parameter since ≤ and ≥ in excel weren't recognized when saved to .txt file

OUT = (f'Success, you can now use your txt file!\
             \n{text_path_output}') # Output the final message to the user

Maybe someone can point in the right direction? I am struggling with this for some time, maybe I am missing a very obvious or simple detail but I cannot figure out what it is.
Thank you in advance.

Hi,
try to change the encoding with

text = np.savetxt(text_path_output, final_concat_txt_file.values, fmt='%s' , delimiter='\t', encoding="utf-16")

or

text = np.savetxt(text_path_output, final_concat_txt_file.values, fmt='%s' , delimiter='\t', encoding="utf-16(LE)")

`

Edit
you can try to use directly your dataframe with df.to_csv()

example

 import clr
import sys
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import Autodesk.DesignScript.Geometry as DS
#import Revit API
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB
#import net library
from System import Array
from System.Collections.Generic import List, IList, Dictionary

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference('Python.Included')
import Python.Included as pyInc
path_py3_lib = pyInc.Installer.EmbeddedPythonHome
sys.path.append(path_py3_lib + r'\Lib\site-packages')

import numpy as np
import pandas as pd

xlsPath = IN[0]
folder = System.IO.Path.GetDirectoryName(xlsPath)
out = folder +"\\test_new_keyNote.txt"

df1 = pd.read_excel(xlsPath)
#
df1.to_csv(out, sep='\t', na_rep='', float_format=None, columns=None, header=False, index=False, index_label=None, mode='w', encoding="utf-16")

OUT = repr(df1)

Thank you for your answer.
Finally I had some time to test your encoding suggestions but unfortunately the result is the same. I can, nevertheless, workaround this issue with a “Wrap between parameters only” label Revit family.

I am avoiding exporting to .csv or .excel formats since the output should be direct to .txt. This way the user can link it directly to Revit.
If I introduce a step in between (excel or csv) the tool acceptance/adoption won’t be the same.

1 Like