Monday, July 24, 2023

Merge excel sheets into a single PDF file

 Here you will find a python code that will merge all excel sheets in a workbook into a single PDF file.

An excel workbook usually contains more than one or more sheets. In this post I want to automate the process of converting several sheets into pdf files then merge those pdf files into one single pdf.


import os
import glob
from PyPDF2 import PdfFileMerger
import win32com.client as client

# ------------------------------------------------
# Convert Excel file to PDF
# ------------------------------------------------
my_xlxs_file = r"C:\Users\`HYJ7\Desktop\XLS to PDF\Sample_quicktransportsolutions.xlsx"

# Open Microsoft Excel in the background
xl_app = client.DispatchEx("Excel.Application")
xl_app.Visible = False
xl_app.DisplayAlerts = False
xl_app.ScreenUpdating = False

pdf_path = os.path.splitext(my_xlxs_file)[0]

# Read Excel File
workbook = xl_app.Workbooks.Open(my_xlxs_file)
sheet_names = [sheet.Name for sheet in workbook.Sheets] # get sheet names

n = 1
for sht in range(len(sheet_names)):
    work_sheet = workbook.Worksheets[sht]

    work_sheet.ExportAsFixedFormat(0, f"{pdf_path}_{n}")
    n += 1

# Close the workbook
workbook.Close()

# ------------------------------------------------
# Merge PDF files into a single file...
# ------------------------------------------------

output_pdf_name = 'TestMergedPDF'
input_dir = r'C:\\Users\\`HYJ7\\Desktop\\XLS to PDF\\'
merge_list = []

for x in os.listdir(input_dir):
    if not x.endswith('.pdf'):
        continue
    merge_list.append(os.path.join(input_dir, x))

merger = PdfFileMerger()

for pdf in merge_list:
    merger.append(pdf)

merger.write(input_dir + f"\\{output_pdf_name}.pdf") #your output directory and pdf_file name
merger.close()

# Delete individual pdf files...
for pdf in merge_list:
    os.remove(pdf)

That is it!

Saturday, July 15, 2023

AutoCAD Programming using AutoLISP

What is LISP

LISP stands for "List Processor". It is a programming language that was developed in the late 1950s by John McCarthy. It is one of the oldest high-level programming languages still in use today. LISP is known for its unique syntax, which is based on nested parentheses and prefix notation.

LISP has found applications in various domains, including artificial intelligence, natural language processing, and symbolic mathematics. It has been used for academic research, commercial software development, and prototyping new programming language concepts. LISP has been influential in the development of other programming languages and has inspired many dialects and variants over the years. Some notable LISP dialects include Common Lisp, Scheme, and Clojure.


What is AutoLISP

AutoLISP is a dialect of the LISP programming language that is specifically designed for extending the capabilities of AutoCAD, a popular computer-aided design (CAD) software. AutoLISP allows users to automate repetitive tasks, create custom commands, and add new functionality to AutoCAD.

AutoLISP is a procedural programming language, which means it follows a step-by-step approach to executing instructions. It provides a set of functions and commands that can be used to interact with AutoCAD's drawing objects, manipulate geometry, modify settings, and perform various operations.

With AutoLISP, you can write scripts and programs that automate tasks such as creating objects, modifying attributes, generating reports, and implementing custom design algorithms. These programs can be executed within the AutoCAD environment, providing users with the ability to tailor AutoCAD to their specific needs and streamline their workflows.

AutoLISP programs are typically written in plain text files with a ".lsp" extension. They can be loaded into AutoCAD using the "Appload" command, which makes the functions and commands defined in the AutoLISP program available for use within the software.

AutoLISP has a rich set of built-in functions for manipulating lists, strings, numbers, and other data types. It also provides control structures such as loops and conditionals to enable decision-making and repetition in your programs. Furthermore, AutoLISP supports the use of variables, user-defined functions, and error handling mechanisms. By leveraging AutoLISP, users can enhance their productivity, automate repetitive tasks, and extend the capabilities of AutoCAD according to their specific requirements.


Structure of AutoLISP Syntax

The syntax of AutoLISP follows the general principles of the LISP programming language but with specific features tailored for use within AutoCAD. Here are some key aspects of AutoLISP syntax:

1. Parentheses: AutoLISP uses parentheses extensively to denote function calls and to structure expressions. Each function call is enclosed in parentheses, with the function name followed by its arguments.

   Example: `(setq x (+ 2 3))`

2. Prefix notation: AutoLISP uses prefix notation, which means the function name precedes its arguments. This differs from traditional infix notation used in many other programming languages.

   Example: `(+ 2 3)` instead of `2 + 3`

3. Lists: AutoLISP treats code and data structures uniformly using lists. A list is enclosed in parentheses and can contain any number of elements, which can be atoms (symbols or numbers) or nested lists.

   Example: `(setq mylist '(1 2 3))`

4. Symbols and variables: Symbols in AutoLISP are used to represent variables, function names, and special operators. They are typically strings of alphanumeric characters and can include special characters like dashes and underscores.

   Example: `(setq radius 5)`

5. Quoting: To prevent the evaluation of expressions, the quote function (') or the backquote syntax (`) is used to quote the following expression. This is useful when you want to treat an expression as data rather than executing it.

   Example: `(setq mylist '(1 2 3))`

6. Functions and special operators: AutoLISP provides a wide range of built-in functions and special operators for performing various operations. Functions are invoked by enclosing the function name and its arguments in parentheses.

   Example: `(setq sum (+ 2 3))`

7. Variables and assignments: Variables in AutoLISP are created using the `setq` function, which stands for "set quote." It assigns a value to a symbol, creating a variable or updating its value.

   Example: `(setq x 10)`

8. Control structures: AutoLISP supports control structures like conditional statements and loops for program flow control. The `if` function is used for conditional branching, while the `repeat` and `while` functions are used for creating loops.

   Example:

   ```

   (if (> x 5)

       (setq result "Greater than 5")

       (setq result "Less than or equal to 5"))

   ```

These are some of the fundamental elements of AutoLISP syntax. Understanding these aspects will help you write AutoLISP programs and extend the capabilities of AutoCAD.

Wednesday, July 5, 2023

Animating SVG maps 101

 Let take a look at how to animate web maps in SVG format. SVG stands for 'Scalable Vector Graphics' and it is a web-friendly vector file format. It defines vector-based graphics in XML format.

Advantages of using SVG over other image formats (like JPEG, PNG and GIF) are:

  • SVG images can be created and edited with any text editor
  • SVG images can be searched, indexed, scripted, and compressed
  • SVG images are scalable
  • SVG images can be printed with high quality at any resolution
  • SVG images are zoomable
  • SVG graphics do NOT lose any quality if they are zoomed or resized
  • SVG is an open standard
  • SVG files are pure XML

SVG images with a drawing program, like Inkscape, Adobe Illustrator etc. Since we will be SVG maps, we can use GIS software like QGIS, ArcGIS etc to create the map then export it to SVG. We can also use online tool like mapshaper to convert GIS map to SVG format.

Whatever method you used in creating you SVG maps is of less importance since all we care in this post is to animate it using CSS.

I will apply the CSS styles right inside the SVG file, so I will surround it with 'Character Data' tag <![CDATA[ ... ]]> to prevent some characters been parsed as part of the SVG XML tags.


1. Change map color on hover and rotate

<style type="text/css" media="screen">
  <![CDATA[

  path {
    cursor: pointer;
    fill: #fff;
    }


  path:hover {
    fill: #ff68ff;

    transition-property: rotate;
    transition-duration: 30s;
    rotate: -90deg;
    }


  ]]>
</style>



2. On hover change map stroke color and stroke width

<style type="text/css" media="screen">
  <![CDATA[

  path {
    cursor: pointer;
    fill: #fff;
    opacity: 1;

    }


  path:hover {
    transition: stroke-width 1s, stroke 1s;

    stroke-width: 8;
    stroke: #000fff;
    }

  ]]>
</style>