What is the correct preposition to use for the author of a book? For example, Hamlet by William Shakespeare. I thought it would be par (Hamlet par William Shakespeare) but I've seen many examples of de (Hamlet de William Shakespeare).
The preposition is de when the book or other work is not mentioned by name.
The preposition is par when the origin of the work is expressed by a verb in the passive voice.
When the title is mentioned, it is common that two interpretations are possible: a work “of” (i.e. by) an author; or a work written (or composed, etc.) by the author, where the verb is omitted.
There can be exceptions, but usually, when the title and the author form a noun phrase, de is used. The preposition par is used when the author indication is a subtitle to the title (it's what you'd put on a book cover, if you put a preposition at all) or a parenthetical remark.
For works that have multiple authors, such as films and songs, the preposition par is rarely used unless the role of the authors is explicited (un film de Renoir vs. un film réalisé par Renoir).
« de » is probably the form you're searching for.
« par » is far less common in this context, it's mostly used for adaptations (a movie adapted from a book, for example) or interpretations (musical or theatrical performance, ...).
But I guess it would be understood anyway, and cannot actually be said "incorrect".
Both par and de are quite accepted, and I can't see any reason or context why you couldn't use one or the other, as long as we are talking about a theater/poetry work, since it bears a bit of a "classical" and old-fashioned connotation.
For any other artistic production (novels, song, painting, movie, etc...) I would tend to use de.