This work aimed to study the diagnostic value of dynamic electrocardiogram (ECG) based on P wave detection algorithm for arrhythmia after hepatectomy in patients with primary liver cancer, and to compare the therapeutic effect of different doses of Betaloc. P wave detection algorithm was introduced for ECG automatic detection and analysis, which can be used for early diagnosis of arrhythmia. Sixty patients with arrhythmia after hepatectomy for primary liver cancer were selected as the research objects. They were randomly divided into control group, SD group, MD group, and HD group, with 15 cases in each group. No Betaloc, low-dose (≤47.5 mg), medium-dose (47.5–95 mg), and high-dose (142.5–190 mg) Betaloc were used for treatment. As a result, P wave detection algorithms can mark P waves that may be submerged in strong interference. P waves from arrhythmia database were used to verify the performance of the proposed algorithm. The prediction precision (Pp) of ventricular arrhythmia and atrial arrhythmia was 98.53% and 98.76%, respectively. Systolic blood pressure (117.35 ± 7.33, 126.44 ± 9.38, and 116.02 ± 8.2) mmHg in SD group, MD group, and HD group was significantly lower than that in control group (140.3 ± 7.21) mmHg after two weeks of treatment. Moreover, those of SD group and HD group were significantly lower than MD group (
P
<
0.05
). The effective rate of cardiac function improvement in SD group (72.35 ± 1.21%) was significantly higher than that in control group, MD group, and HD group (38.2 ± 0.98%, 65.12 ± 1.33%, and 60.43 ± 1.25%;
P
<
0.05
). In short, dynamic ECG based on P wave detection algorithm had high diagnostic value for arrhythmia after hepatectomy in patients with primary liver cancer. It was safe and effective for patients to choose small dose of Betaloc.