Package: pyfastexcel
pyfastexcelThis package enables high-performance Excel writing by integrating with the
streaming API from the golang packageexcelize. Users can leverage this
functionality without the need to write any Go code, as the entire process
can be accomplished through Python.InstallationInstall via pip (Recommended)You can easily install the package via pippipinstallpyfastexcelInstall manuallyIf you prefer to build the package manually, follow these steps:Clone the repository:gitclonehttps://github.com/Zncl2222/pyfastexcel.gitGo to the project root directory:cdpyfastexcelInstall the required golang packages:gomoddownloadBuild the Golang shared library using the Makefile:makeInstall the required python packages:pipinstall-rrequirements.txtorpipenvinstallImport the project and start using it!FeaturesPython and Golang Integration: Seamlessly call Golang built shared
libraries from Python.No Golang Code Required: Users can solely rely on Python for Excel file
generation, eliminating the need for Golang expertise.How it WorksThe core functionality revolves around encoding Excel cell data and styles,
or any other Excel properties, into a JSON string within Python. This JSON
payload is then passed through ctypes to a Golang shared library. In Golang,
the JSON is parsed, and using the streaming writer ofexcelizeto wrtie excel in
high performance.UsageThe current version can only be used with this library through example
snippets or the example.py file in the root directory of this repository.
Seelimitationsfor more details.The steps are:Create a class for yourstyleregisted likeStyleCollectionsin the example.Create a class for your excel creation implementation and inheritNormalWriterorFastWriterandStyleCollections.Implement your data writing logic indef _create_body()anddef _create_single_header()(The latter is not necessary)fromopenpyxl_style_writerimportCustomStylefromopenpyxl.stylesimportSidefrompyfastexcel.driverimportFastWriter,NormalWriterdefprepare_example_data(rows:int=1000,cols:int=10)->list[dict[str,str]]:importrandomrandom.seed(42)headers=[f'Column_{i}'foriinrange(cols)]data=[[random.random()for_inrange(cols)]for_inrange(rows)]records=[]forrowindata:record={}forheader,valueinzip(headers,row):record[header]=str(round(value*100,2))records.append(record)returnrecordsclassStyleCollections:black_fill_style=CustomStyle(font_size='11',font_bold=True,font_color='F62B00',fill_color='000000',)green_fill_style=CustomStyle(font_size='29',font_bold=False,font_color='000000',fill_color='375623',)test_fill_style=CustomStyle(font_params={'size':20,'bold':True,'italic':True,'color':'5e03fc',},fill_params={'patternType':'solid','fgColor':'375623',},border_params={'left':Side(style='thin',color='e12aeb'),'right':Side(style='thick',color='e12aeb'),'top':Side(style=None,color='e12aeb'),'bottom':Side(style='dashDot',color='e12aeb'),},ali_params={'wrapText':True,'shrinkToFit':True,},number_format='0.00%',)classPyExcelizeNormalExample(NormalWriter,StyleCollections):defcreate_excel(self)->bytes:self._set_header()self._create_style()self.set_file_props('Creator','Hello')self._create_single_header()self._create_body()returnself._read_lib_and_create_excel()def_set_header(self):self.headers=list(self.data[0].keys())def_create_single_header(self):forhinself.headers:self.row_append(h,style='green_fill_style')self.create_row()def_create_body(self)->None:forrowinself.data:forhinself.headers:ifh[-1]in('1','3','5','7','9'):self.row_append(row[h],style='black_fill_style')else:self.row_append(row[h],style='test_fill_style')self.create_row()self.switch_sheet('Sheet2')forrowinself.data:forhinself.headers:ifh[-1]in('1','3','5','7','9'):self.row_append(row[h],style=self.green_fill_style)else:self.row_append(row[h],style='black_fill_style')self.create_row()if__name__=='__main__':data=prepare_example_data(653,90)excel_normal=PyExcelizeNormalExample(data).create_excel()file_path='pyexample_normal.xlsx'withopen(file_path2,'wb')asfile:file.write(excel_normal)Current Limitations & Future PlansProblem 1: Dependence on Other Excel PackageLimitations:This project currently depends on the `CustomStyle`` object of
theopenpyxl_style_writerpackage, which is built for openpyxl to write styles in write-only
mode more efficiently without duplicating code.Future Plans:This project plans to create its ownStyleobject, making it no longer
dependent on the mentioned package.Problem 2: Inflexible UsageLimitations:The current version only has the function to write a cell with a style using
the class-based inheritance streaming writer method, similar to theAdvanced Usageinopenpyxl_style_writer.
This means users must inherit theNormalWriterorFastWriterclasses and
also yourStyleCollectionsto correctly register and use the Style. In short,
if you want to use this library, you have to createStyleCollectionsandYour-Writer-Classand implement the excel creation inYour-Writer-Classas
shown in the code snippet provided.fromopenpyxl_style_writerimportCustomStyleclassStyleCollections:black_fill_style=CustomStyle(font_size='11',font_bold=True,font_color='F62B00',fill_color='000000',)test_fill_style=CustomStyle(font_size='19')classPyExcelizeNormalExample(NormalWriter,StyleCollections):headers=['col1','col2','col3']defcreate_excel(self)->None:forrowinself.data:forhinself.headers:ifh[-1]in('1','3','5','7','9'):self.row_append(row[h],style='black_fill_style')else:self.row_append(row[h],style='test_fill_style')self.create_row()if__name__=='__main__':data=[{'col1':1,'col2':2,'col3':3},{'col1':4,'col2':5,'col3',6}]excel=PyExcelizeNormalExample(data).create_excel()withopen('example.xlsx','wb')asfile:file.write(excel)Future Plans:Make the style register in the shared class object on the Python side.
Also, create a function to register the style. By doing so, the style
won't need to depend on thecustom-writer-class. All the styles registered
through that function will be sent to Golang and registered.Add the ability to create a cell and style with the index, similar
to what openpyxl does. (The code snippet provided is only an example,
not the real implementation method planned)ws['A1'].value='test'ws['A1'].style='black_fill_style'