It is not known when Zhang Wenwei was born. His family claimed ancestry from the early Han Dynasty general Zhang Er (張耳). The traceable ancestry of Zhang Wenwei's included officials of Sui Dynasty and Tang Dynasty. His grandfather Zhang Junqing (張君卿) was a copyeditor at Jixian Pavilion (集賢院), and his father Zhang Shang (張裼) served in a number of prominent positions, including, eventually, the military governorship (Jiedushi) of Tianping Circuit (天平, headquartered in modern Tai'an, Shandong). Zhang Wenwei had at least two younger brothers, Zhang Jimei (張濟美) and Zhang Yixian (張貽憲), both of whom, like he, would eventually pass the imperial examinations in the Jinshi (進士) class. However, only Zhang Yixian would go on to serve as an imperial official; Zhang Jimei would be stricken by mental ailments, and it was said that Zhang Wenwei, while eventually progressing in his career, would care for Zhang Jimei's health for over 30 years, drawing praise from the gentry of the day. Zhang Wenwei himself was said to be good at writing in his youth, and he was both studious and friendly with people, gaining the reputation of being a gentleman.
Zhang Wenwei passed the imperial examinations in the Jinshi class in 875, during the reign of Emperor Xizong. He thereafter served as a surveyor for the office of salt and iron monopolies; he then served as a sheriff of a county near the imperial capital Chang'an, as well as a scholar at an imperial institute. After his father Zhang Shang died in 877, he left governmental service to observe a mourning period and to attend to the household.
Sometime during Emperor Xizong's Zhonghe era (881-885), when Emperor Xizong had fled to Chengdu due to the major agrarian rebellion led by Huang Chao, who captured Chang'an and declared his own state of Qi as its emperor, Emperor Xizong, in order to raise funds for the military campaigns against Qi, moved the office of salt and iron monopolies to Yang Prefecture (揚州, in modern Yangzhou, Jiangsu) and put the official Li Du (李都) in charge of the office. At Li Du's recommendation, Zhang Wenwei was made a roving surveyor for the office. After Huang was defeated and Emperor Xizong returned to Chang'an in 885, Zhang was first made an imperial censor with the title Jiancha Yushi (監察御史); then (either successively or holding the offices all at once) Zuo Bujue (左補闕), a low-level advisory official at the examination bureau of government (門下省, Menxia Sheng); an imperial censor with the title Dianzhong Shi Yushi (殿中侍御史); an imperial chronicler with the title Qiju Sheren (起居舍人); and Sixun Yuanwailang (司勛員外郎), a low-level official at the ministry of civil service affairs (吏部, Libu). He was then promoted to a supervisory position at the ministry of civil service affairs, with the title Sixun Langzhong (司勛郎中) and put in charge of drafting edicts. After serving in those roles for a year, he was made Zhongshu Sheren (中書舍人). After his mother's death, he left governmental service to observe a mourning period for her.
After Zhang finished observing the mourning period, he returned to the imperial government as a Zhongshu Sheren. He was soon thereafter made the chief imperial scholar (翰林學士承旨, Hanlin Xueshi Chengzhi). As then-reigning Emperor Zhaozong (Emperor Xizong's brother and successor) had just returned to Chang'an, and the imperial authority was extremely weak at that time, edicts that Zhang drafted had to be, and were, appropriately delicate in language but assertive, and was much praised for their propriety. He was then given the additional post of deputy minister of census (戶部侍郎, Hubu Shilang), and later was made the deputy minister of rites (禮部侍郎, Libu Shilang) but no longer imperial scholar by that point.
As of 905, Zhang was still serving as the deputy minister of rites—at that time, at Luoyang, which had been made the capital after the powerful warlord Zhu Quanzhong the military governor of Xuanwu Circuit (宣武, headquartered in modern Kaifeng, Henan) had forcibly moved Emperor Zhaozong from Chang'an to Luoyang in 904,—under Emperor Zhaozong's son and successor Emperor Ai, as Zhu had had Emperor Zhaozong assassinated late in 904. In 905, the chancellor Liu Can, an associate of Zhu's, had alienated Zhu from the other chancellors Pei Shu, Dugu Sun, and Cui Yuan; Pei, Dugu, and Cui were therefore removed from chancellor offices, while Zhang and Yang She were made chancellors, with the designation Tong Zhongshu Menxia Pingzhangshi (同中書門下平章事). He was also made Zhongshu Shilang (中書侍郎), the deputy head of the legislative bureau, and acting director of taxation (判戶部, Pan Hubu). Later in the year, when Zhu carried out a purge of many prominent Tang officials at the instigation of Liu and Li Zhen (in which, among others, Pei, Dugu, Cui, and other former chancellors Lu Yi and Wang Pu were killed), Liu had initially prepared an even larger list of officials to purge. It was said that it was at Zhang's urging that the purge was not expanded. After Liu himself was killed by Zhu around the new year 906, Zhang also took over the duties of the directors of finances and salt and iron monopolies.
In 907, Emperor Ai was preparing to yield the throne to Zhu Quanzhong. As part of the ceremonial passage of the throne, Zhang, acting as the head of the legislative bureau (中書令, Zhongshu Ling), was put in charge of announcing the passage of the throne. He, as well as Yang (who was to deliver the main imperial seal to Zhu) and Xue Yiju (who was to deliver the secondary imperial seals), went in a grand procession from Luoyang to Daliang (i.e., the capital of Xuanwu Circuit), where Zhu was at the time, for the ceremony. Zhu accepted the throne, ending Tang and starting a new Later Liang (as its Emperor Taizu). It was said that, at a subsequent feast that the new emperor held for Zhang and the other Tang officials, most of them, including Zhang, were prostrate and unable to speak.
After the transition to Later Liang, Zhang remained chancellor. He was said to be the drafter of much of the regulations that the Later Liang imperial government used. When Emperor Taizu left Luoyang in 908 to battle the state of Jin (i.e., the lands ruled by Li Cunxu the Prince of Jin, who refused to recognize the new Later Liang regime), he left Zhang in charge of Luoyang. Zhang died later in 908, while still serving as a Later Liang chancellor.