The design of district heating and cooling (DHC) networks is crucial to increase the pooling of energy supply and demand between individual buildings, and thus to reduce the environmental and financial costs of the energy systems. DHC networks are also useful to increase the share of renewable energy sources in cities, their integration being more challenging in high-density environments. This paper presents a novel simulation framework for the optimization of building energy systems connected to a district heating and cooling network. The developed method is based on the open-source Python library PyPSA and is adapted for the early-design exploration of multiple scenarios and their optimization, based on GIS input data. We show the application of the proposed method into a fictitious district in France composed of mixed-use buildings. The results compare two scenarios of energy systems minimizing either greenhouse gas emissions or the energy cost.